简体   繁体   English

如何将变量值分配给另一个变量,而不是引用dojo javascript

[英]how to assign a variable value to another variable instead of reference dojo javascript

I m trying to change stores of dijit/form/select on fly. 我想即时更改dijit / form / select的存储。 For that purpose, I need to retrieve a store from server. 为此,我需要从服务器检索商店。 I need to save this store in some temporary store, and make changes to temporary store in my code. 我需要将此存储保存在某个临时存储中,并在我的代码中对临时存储进行更改。

In my code below, requiredStore has the data received from server. 在下面的代码中,requiredStore具有从服务器接收的数据。 I need to create a new temporary store, in my case temporaryStore, that can be assiged data in requiredStore.(instead of temporaryStore being a reference to requiredStore)So that, when I remove/change values in temporaryStore, requiredStore is not affected. 我需要创建一个新的临时存储区(在我的情况下为临时存储区),该存储区可以作为requireStore中的辅助数据。(而不是临时存储区是对requiredStore的引用),因此,当我删除/更改临时存储区中的值时,requiredStore不会受到影响。 The following is the code, I have used. 以下是我使用过的代码。

        function getDropDownContents() {
            require([
            "dojo/store/Memory",
            "dijit/form/Select",
            "dojo/data/ObjectStore",
            "dojo/request",
            "dojo/domReady!"
            ],
            function (Memory, Select, ObjectStore, request) {
                var os;
                var def = new dojo.Deferred();
                dojo.xhrGet({
                    url: "pageToGetValues.aspx",
                    handleAs: "json",
                    load: function (res) {
                        requiredStore = new Memory({ data: res });
                        temporaryStore = requiredStore;
                        getOptionsToSelect('dropdown1');
                        var select = dijit.byId('dropdown1');
                        select.on('change', function (evt) {
                            getOptionsToSelect('dropdown2');
                        });
                        myFormDialog.show();
                    }
                });
            });
        }

But, I don't know, what javaScript principles I m not following, coz , when I m changing temporaryStore, requiredStore is also getting changed. 但是,我不知道,当我更改临时存储时,我并没有遵循什么javaScript原理,因为coz也会被更改。

You need to either do a deep clone of requiredStore (going through properties and prototype to create a perfect duplicate), or else just create a different memory store altogether: 您需要做一个requiredStore的深层克隆(遍历属性和原型以创建完美的副本),或者只创建一个完全不同的内存存储:

requiredStore = new Memory({ data:res });
temporaryStore = new Memory({ data: res });

I don't know why you wouldn't use this approach, but if you want to do a deep clone this question will help: 我不知道为什么不使用这种方法,但是如果您想进行深层克隆,则此问题将有所帮助:

What is the most efficient way to deep clone an object in JavaScript? 在JavaScript中深度克隆对象的最有效方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM