最近我尝试使用sencha touch从json文件获取数据,但显示0结果,请参考下面的编码并告诉我有关我的错误,
谢谢,
app.js
Ext.application({
name: 'TP',
views: [
'Main'
],
models: [
'User'
],
stores: [
'Users'
],
launch: function() {
// Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();
// Initialize the main view
Ext.Viewport.add(Ext.create('TP.view.Main'));
var user = Ext.create('TP.model.User', {
name: 'James Henry',
age: 24,
phone: '555-555-5555',
username: 'Admin'
});
Ext.getStore('Users').on('load', this.onStoreLoad, this);
console.log(Ext.getStore('Users'));
},
onStoreLoad: function(self, records, success, operation){
console.log(self);
}
});
Model :: User.js
Ext.define('TP.model.User',{
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'},
{name: 'phone', type: 'string'},
{name: 'gender', type: 'string'},
{name: 'username', type: 'string'},
{name: 'alive', type: 'boolean', defaultValue: true}
],
validations: [
{type: 'presence', field: 'age'},
{type: 'length', field: 'name', min: 2},
{type: 'inclusion', field: 'gender', list: ['Male', 'Female']},
{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},
{type: 'format', field: 'username', matcher: /^[A-Za-z0-9 _]*$/ }
],
},
ageString: function(){
var age = this.get('age');
if(age > 1) {
return age + " yesrs old";
}else{
return age = " Yes old";
}
}
});
Store :: Users.js
Ext.define('TP.store.Users', {
extend: 'Ext.data.Store',
config: {
model: 'TP.model.User',
autoload: true,
proxy: {
type: 'ajax',
url: 'http://localhost/sencha/SenchaStarter/data/users.json',
reader: {
rootProperty: 'users',
type: 'json'
}
}
}
});
JSON文件users.json
{
"users": [
{
"name": "Mike Henderson",
"age": 24,
"phone": "555-555-555",
"gender": "Male",
"username": "mhenderson",
"alive": true
},
{
"name": "Sally Michael",
"age": 34,
"phone": "555-555-555",
"gender": "Female",
"username": "sallym",
"alive": true
},
{
"name": "Rory Muldoon",
"age": 19,
"phone": "555-555-555",
"gender": "Male",
"username": "greatscott",
"alive": true
}
]
}
答案 0 :(得分:2)
商店未加载,因为您尚未加载它。原因是您在商店中编写此配置选项时输入错误:
autoload
需要的时候:
autoLoad
答案 1 :(得分:1)
您商店的代理设置为ajax。它不会保存记录。我相信你只能访问app.js中onStoreLoad中的记录。
你或许(现在这只是一个理论)在你的模型中设置ajax代理(我确信你可以这样做并且是sencha推荐的,因为你可以在需要的时候用商店代理覆盖这个代理{{ 3)})然后将记录保存在商店中,因为我认为默认情况下商店代理类型是localstorage。
祝你好运:)