Search code examples
model-view-controllerdojodojox.mobiledojox.app

Dojox/app keeping models synchronized with store


I am currently using dojox/app to create a SPA, and I am starting now to add stores and models. I have been able to create a store from a json object, to create a model from the store, and to bind fields to the model using dojox mvc. However, I have something that I have not yet been able to do: update a 2nd model that is binded to the same store as the 1st one.

I will give an example. I have this store:

        win.global.modelApp = {}; 
        modelApp.names = {
            identifier: "id",
            items: [{
                "id": 1,
                "First": "John",
                "Last": "Doe",
            },{
                "id": 2,
                "First": "John2",
                "Last": "Doe",
            }]
        }

and then I create two models using this store in the config.json file:

"stores": {
    "namesStore":{
        "type": "dojo/store/Memory",
        "params": {
            "data": "modelApp.names"
        }
    }
},

"models": {
    "namesXUnused": {
        "modelLoader": "dojox/app/utils/mvcModel",
        "type": "dojox/mvc/EditStoreRefListController",
        "params":{
            "store": {"$ref":"#stores.namesStore"}
        }
    },
    "namesXUnused2": {
        "modelLoader": "dojox/app/utils/mvcModel",
        "type": "dojox/mvc/EditStoreRefListController",
        "params":{
            "store": {"$ref":"#stores.namesStore"}
        }
    }
}

Then, in my HTML file, I have a field binded to namesXUnused (property First), and another binded to namesXUnused2 (property First). When I edit the first field, I then have a button that commits these changes to the store. I can see via debugger that the store data has been correctly updated. However, I cannot get the second field to reflect the changes. Is there a way to refresh or recreate the model from the store?

Thank you,


Solution

  • I think you need to set "observable": true, on the store to use an Observable store since you want to have updates to the store be reflected back to the models. There are examples in the dojox/app/tests (and the dojox/mvc/tests) if you have problems, but just adding: "observable": true, to your namesStore should do it. "stores": { "namesStore":{ "type": "dojo/store/Memory", "observable": true, "params": { "data": "modelApp.names" } } },

    Regards, Ed