简体   繁体   中英

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. 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. 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. 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.

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 = 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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