我在EXT 6 Beta中创建了一个测试应用程序,我在填充商店时遇到了问题。我试图在Ext.application的启动函数中使用测试数据填充商店。代码的主要部分如下,存在各种console.log。
Ext.define('myApp.Application', {
extend: 'Ext.app.Application',
name: 'myApp',
// stores: ['Items'],
requires: [ 'myApp.store.Items' ],
launch: function () {
var createTestData = function(){ ... }
var testData = createTestData();
this.getStore('Items').add(testData);
console.log(2,this.getStore('Items').getData());
}
});
商店:
Ext.define('myApp.store.Items', {
extend: 'Ext.data.Store',
alias: 'store.items',
model: 'myApp.Item',
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});
网格:
Ext.define('myApp.view.main.ItemList', {
extend: 'Ext.grid.Panel',
requires: [ 'myApp.store.Items' ],
title: 'Items',
store: { type: 'items' },
columns: [ ... ],
listeners: {
afterrender: function(){
console.log(1, this.getStore().getData());
var self = this;
setTimeout(
function(){
console.log(3, self.getStore().getData());
}, 1000);
}
}
});
控制台按以下顺序记录输出:1,2,3。数字1有数据,但2和3都没有。
我尝试将商店设置为单例,但这只会抛出错误,说单例无法实例化。如何将数据保留在商店中或重复访问同一商店?
答案 0 :(得分:1)
如评论中所述,问题是网格创建了一个新的商店而不是使用与Ext.application相同的商店。所以要解决这个问题:
该应用:
Ext.define('myApp.Application', {
...
stores: ['Items'],
// requires: [ 'myApp.store.Items' ],
...
});
网格:
Ext.define('myApp.view.main.ItemList', {
...
requires: [
'myApp.store.Items'
],
store: 'Items', // Refers to the store ID
// store: { type: 'items' }, (Created a new store)
...
});
商店:
Ext.define('myApp.store.Items', {
...
id: 'Items',
...
});