In Sencha Touch, how do I notify all stores associated with a model after calling MyApp.MyModel.load()

109 views Asked by At

How do I notify all stores associated with a model after calling MyApp.MyModel.load()? Alternatively, is there a way to load 1 record by Id from the context of a store?

My ultimate goal is to fetch one single record from the server and refresh a list that is bound to a store that has the record in it.

2

There are 2 answers

3
Alexander On BEST ANSWER

Don't do a Myapp.model.load. Use store.load instead.

Because what you really want to do is this:

function refreshSingleItem(store,id) {
    store.remove(store.getById(id));
    store.getProxy().setExtraParam("id",id);
    store.load({addRecords:true;});
}
1
Alexander On

When you load a store, all records are loaded into that store. You wouldn't want anything else, because you would not have to load everything over the wire if you don't store it on the client side.

It is not possible to do one call and have all stores updated which have that call in their url. It will only update one store, everything else you will have to code by yourself.

That said, you can always make a second store with the same model, and load a single record from the first store into the second:

store1.on('load',function() {
    store2.add(store1.getById(recordId));
});

VoilĂ , now you have a store with only one record and you can load any other record as desired. But this is needed less often than the other way round. So let's say you have a record:

data:[{
    meetingId:123,
    time:'2014-09-16T17:40',
    location:'1 Washington Drive, room #1203'
    participants:[{
        name:'John Doe',
        phone:'46318123'
    },{
        name:'Jane Doe',
        phone:'477119524'
    },{
        name:'George Washington',
        phone:null
    }]
}]

You could then define participants as a field without type in model1, make a model2 with name and phone, and do the following:

store1.on('load',function() {
    form.loadRecord(store1.getAt(0));
    store2.add(store1.getAt(0).get("participants"));
});