我正在玩sencha并创建了一个包含商店和模型的列表:
主:
Ext.Loader.setConfig({enabled: true});
Ext.application({
viewport: {
autoMaximize: false
},
name: 'Sencha',
launch: function() {
Ext.create('Ext.List', {
fullscreen: true,
store: Ext.create('store.Test', {
}),
itemTpl: '{lastName}, {firstName} Age: {age}'
});
}
});
存储
Ext.define('store.Test', {
extend: 'Ext.data.Store',
model: 'model.User',
data: [
{ firstName: 'Ed', lastName: 'Spencer', age: 30 },
{ firstName: 'Tommy', lastName: 'Maintz', age: 41 },
{ firstName: 'Aaron', lastName: 'Conran', age: 24 },
{ firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});
模型:
Ext.define('model.User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'firstName', type: 'string' },
{ name: 'lastName', type: 'string' }
]
});
到目前为止一切正常。
然后我创建了第二个商店,它有另一个字段'age'。我现在想要的是将参数'model'传递给商店,以便能够在不同模型之间“切换”。 我试过这样的事情:
存储
Ext.define('store.Test', {
extend: 'Ext.data.Store',
myModel: null,
constructor : function(model) {
myModel = model;
},
model: this.myModel,
data: [
{ firstName: 'Ed', lastName: 'Spencer', age: 30 },
{ firstName: 'Tommy', lastName: 'Maintz', age: 41 },
{ firstName: 'Aaron', lastName: 'Conran', age: 24 },
{ firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});
并在主文件中:
[...]
store: Ext.create('store.Test', {
model: 'model.UserWithAge'
}),
[...]
但谷歌Chrome给了我:
Uncaught TypeError: Object [object Object],[object Object],[object Object],[object Object] has no method 'getRange'
任何提示我做错了什么?
答案 0 :(得分:0)
没关系,这比我想象的容易得多。 而不是搞乱构造函数或类似的东西,只需在创建商店时传递模型就足够了:
主:
[...]
store: Ext.create('store.Test', {
model: 'model.UserWithAge'
}),
[...]
存储
Ext.define('store.Test', {
extend: 'Ext.data.Store',
model: 'model.User', //only set in case that model is not passed when created
data: [
{ firstName: 'Ed', lastName: 'Spencer', age: 30 },
{ firstName: 'Tommy', lastName: 'Maintz', age: 41 },
{ firstName: 'Aaron', lastName: 'Conran', age: 24 },
{ firstName: 'Jamie', lastName: 'Avins', age: 76 }
]
});