ExtJS 4.1:如何在单个商店中将本地数据与ajax加载的数据组合在一起? [英] ExtJS 4.1 : How to combine local data with ajax loaded data in a single store?
问题描述
我正在寻找一种将本地数据与单个存储中的加载数据组合在一起的方法。我很难用英文解释这个问题,希望这段代码更加明确:
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由/ read返回: [{id:2,name :'Jack'}]
。
Json returned by "/read" : [{ id: 2, name: 'Jack' }]
.
期望的行为: store.count()// 2
推荐答案
您可以使用 .load({addRecords:true}
将加载的记录添加到现有记录中。
You can use .load({addRecords: true}
to add loaded records to existing records.
当然,如果您再次加载 addRecords:true
选项,则会将记录添加到现有记录再次导致:
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' }]
你可以实现一个加载处理程序将存储重置为原始数据,如果只需要 [{id:1,name:'John'},{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.
这篇关于ExtJS 4.1:如何在单个商店中将本地数据与ajax加载的数据组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!