您好我创建了一个商店连接到contacts.json无法正常工作,这是我的商店
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url: 'contacts.json',
reader: {
type: 'json'
}
}
}
});
这是我的模态
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
config: {
fields: ['firstName', 'lastName']
}
});
这是我的json文件
[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
它没有显示数据的错误
而不是代理直接使用它工作的数据
例如:
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.Ajax'],
alias: 'store.List',
config: {
model: 'senchatest.model.Contact',
data :[
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}
});
此代码正常运行,但代理代码无法解决问题
答案 0 :(得分:0)
如下修改模型(不需要config
属性):
Ext.define('senchatest.model.Contact', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'}
]
});
编辑:
我在Sencha上创建一个小提琴,检查出来。
答案 1 :(得分:0)
看看http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Reader,它有一个完美的例子,说明如何做你想做的事情
存储
Ext.define('senchatest.store.List', {
extend: 'Ext.data.Store',
alias: 'store.List',
// use the model @Oğuz Çelikdemir suggested
model: 'senchatest.model.Contact',
proxy: {
type: 'ajax',
url : 'contacts.json',
reader: {
type: 'json',
root: 'contacts'
}
},
});
contacts.json
{
"success": true,
"contacts": [
{ "firstName": "Tommy", "lastName": "Maintz" },
{ "firstName": "Ed", "lastName": "Spencer" },
{ "firstName": "Jamie", "lastName": "Avins" },
{ "firstName": "Aaron", "lastName": "Conran" },
{ "firstName": "Dave", "lastName": "Kaneda" },
{ "firstName": "Michael", "lastName": "Mullany" },
{ "firstName": "Abraham", "lastName": "Elias" },
{ "firstName": "Jay", "lastName": "Robinson" },
{ "firstName": "Zed", "lastName": "Zacharias "}
]
}
答案 2 :(得分:0)
你可以使用
autoLoad: true,
proxy: {
type: 'ajax',
url : 'contacts.json'
}