ExtJS 4.1 : How to combine local data with ajax loaded data in a single store?

ぃ、小莉子 提交于 2019-12-22 09:45:34

问题


I'm looking for a way to combine local data with ajax loaded data in a single store. It's difficult for me to explain this in english, I hope this piece of code will be more explicit :

var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    fields: ['id', 'name'],
    proxy: { type: 'ajax', api: { read: '/read' }  },
    data: [{ id: 1, name: 'John' }]
});

Json returned by "/read" : [{ id: 2, name: 'Jack' }].

Desired behaviour : store.count() // 2


回答1:


You can use .load({addRecords: true} to add loaded records to existing records.

Of course if you load again with the addRecords: true option enabled it will add the records to the existing records again resulting in:

[{ id: 1, name: 'John' },{ id: 2, name: 'Jack' },{ id: 2, name: 'Jack' }]

You could implement a before load handler to reset the store to the original data and load again if you want only [{ id: 1, name: 'John' },{ id: 2, name: 'Jack' }] every time you load again.



来源:https://stackoverflow.com/questions/12408952/extjs-4-1-how-to-combine-local-data-with-ajax-loaded-data-in-a-single-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!