从嵌套的 json 中读取数据
我之前的帖子也是如此。这是我从服务器获取的更改后的 json。
{"property":[{"@length":"10","@mandatory":"true","@type":"String","@label":"Description","@id":"desc"},{"@mandatory":"false","@type":"Number","@label":"Code","@id":"code"},{"@mandatory":"true","@type":"Boolean","@label":"Check","@id":"check"},{"@mandatory":"true","@type":"DateField","@label":"Date","@id":"date"},{"@mandatory":"true","@type":"List","@label":"Types","@id":"types","options":[{"@value":"eee"},{"@value":"fff"}]}]}
我想在组合框中呈现类型的选项数组。这就是模型。
Ext.define('Property', {
extend : 'Ext.data.Model',
fields : [ {
name : 'id',
mapping : '@id'
}, {
name : 'label',
mapping : '@label'
}, {
name : 'type',
mapping : '@type'
}, {
name : 'mandatory',
mapping : '@mandatory'
}, {
name : 'length',
mapping : '@length'
} ],
// {name : 'values', mapping : 'options[0].@value'}], didnt work
hasMany : {
model : 'Options',
name : 'options'
}
});
Ext.define('Options', {
extend : 'Ext.data.Model',
// reader : {
type : 'json',
root : 'options'
},
fields : [ {
name : 'value1',
mapping : '@value'
} ],
associations : [ {
type : 'belongsTo',
model : 'Property'
} ]
});
无法找到出了什么问题。有人可以建议我正确的做法吗?
Its same regarding my previous post. This is the changed json I am getting from the server.
{"property":[{"@length":"10","@mandatory":"true","@type":"String","@label":"Description","@id":"desc"},{"@mandatory":"false","@type":"Number","@label":"Code","@id":"code"},{"@mandatory":"true","@type":"Boolean","@label":"Check","@id":"check"},{"@mandatory":"true","@type":"DateField","@label":"Date","@id":"date"},{"@mandatory":"true","@type":"List","@label":"Types","@id":"types","options":[{"@value":"eee"},{"@value":"fff"}]}]}
I want to render the options array of Types in a combo-box.This is the model.
Ext.define('Property', {
extend : 'Ext.data.Model',
fields : [ {
name : 'id',
mapping : '@id'
}, {
name : 'label',
mapping : '@label'
}, {
name : 'type',
mapping : '@type'
}, {
name : 'mandatory',
mapping : '@mandatory'
}, {
name : 'length',
mapping : '@length'
} ],
// {name : 'values', mapping : 'options[0].@value'}], didnt work
hasMany : {
model : 'Options',
name : 'options'
}
});
Ext.define('Options', {
extend : 'Ext.data.Model',
// reader : {
type : 'json',
root : 'options'
},
fields : [ {
name : 'value1',
mapping : '@value'
} ],
associations : [ {
type : 'belongsTo',
model : 'Property'
} ]
});
Unable to find what is going wrong. Can someone suggest me the proper way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该研究模型中的“关联”概念,并使用 hasMany/belongs 关系。恕我直言,这将使代码变得更简单、更清晰,同样,当您需要使用您要设置的数据时,代码稍后会更有用。
有关关联的更多信息,请参阅 sencha 文档
HTH!
I think you should look into the "associations" concept in the models, and use the hasMany/belongs to relationships. IMHO, it would make it easier and cleaner code, in the same way more usefull later in the code when you will need to use the data you're trying to set.
FOr more info about association please refer to this great link in sencha docs
HTH!
您如何配置您的组合商店?如果您的数据已获取,您只需通过访问返回到商店的原始数据来设置组合的数据。
How are you configuring your combo store? If your data was fetched already you can just set the data for the combo by accessing raw data that was returned to the store.