我从服务器获得下一个JSON:
{
"contextPath":"http://apps.dhis2.org/demo",
"user":{
"id":"GOLswS44mh8",
"name":"System Administrator",
"isAdmin":true,
"ou":{
"id":"ImspTQPwCqd",
"name":"Sierra Leone"
}
}
}
我需要在两个模型中转换这个JSON:用户模型和模型。
我阅读了本教程http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.reader.Reader,因此它是我的代码:
用户模型:
Ext.define('mobile-visualizer.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'},
{name: 'isAdmin', type: 'boolean'}
],
hasOne: {
model: "mobile-visualizer.model.OrganizationUnit",
name: "ou"
},
proxy: {
type: 'ajax',
url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
method : 'GET',
withCredentials : true,
useDefaultXhrHeader : false,
reader: {
type: 'json',
rootProperty: 'user'
}
}
}
});
OrganizationUnit模型:
Ext.define('mobile-visualizer.model.OrganizationUnit', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'}
],
belongsTo: 'mobile-visualizer.model.User'
}
});
Store:
Ext.define('mobile-visualizer.store.UsersStore', {
extend : 'Ext.data.Store',
model : "mobile-visualizer.model.User",
autoLoad : false,
storeId : "usersStore",
proxy : {
type : 'ajax',
url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
method : 'GET',
withCredentials : true,
useDefaultXhrHeader : false,
reader : {
type : 'json',
rootProperty : 'user'
}
}
});
因此,当我尝试使用其他代码从商店中获取用户时:
var store = Ext.create('mobile-visualizer.store.UsersStore');
store.load({
callback : function() {
// the user that was loaded
var user = store.first();
console.log(user.ou())
}
});
我有错误:Uncaught : Object object没有方法'ou'
我可以获得关于用户的所有信息,但不能从用户获取ou。看上去像是某种关联问题。
但我所做的一切都像官方教程一样。请帮助解决这个问题。谢谢。
发布于 2013-06-26 21:02:48
遵守我的规则,一切都会好起来的:
http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html
http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html
这些是用于extjs,但非常类似的感应器触摸。
你忘了associationKey.
此外,不需要在存储中重新定义代理,因为它将继承其模型的代理。
而且,用户可能属于一个组织单元,而没有一个.
https://stackoverflow.com/questions/17309325
复制相似问题