0

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.

John Doe
  • 1,005
  • 2
  • 8
  • 23

2 Answers2

1

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;});
}
Alexander
  • 19,906
  • 19
  • 75
  • 162
  • Thanks Alexander! This seems like a great idea. What does addRecords: true do? Also, is there a way to maybe not remove but overwrite the existing record? I have a list that is bound to this store, and if I remove the item, the list will omit the display of that record until the record is fetched from the server. – John Doe Sep 20 '14 at 20:32
  • If you submit the id on your records (the id has to be part of the model and `model.idProperty` has to be set correctly), you should also be able to overwrite without removing the record before. Just try it out. `{addrecords:true}` means that the records in the store will be kept, normally they would be deleted. – Alexander Sep 20 '14 at 21:42
  • Thanks @Alexander ! This worked! I didn't need a remove() but I did need the addRecords: true to keep the existing records as you said. Thanks again! – John Doe Sep 21 '14 at 07:42
0

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"));
});
Alexander
  • 19,906
  • 19
  • 75
  • 162
  • Thank you for your reply! I'm not sure I understand what you tried to do with the 2 stores. Say I have a Books store, and I have 3 books in it. I get a push notification on my iPhone that Book #2 has been updated. So, I do MyApp.model.Book.load(); But, my UI shows a list that is bound to the Books store, and I'd like the list to now only update Book #2's changed field (say downloadCount). How can I use the method you laid out to achieve that? – John Doe Sep 14 '14 at 14:54