Javascript 如何在Sencha Touch的selectfield中实现嵌套和关联的json模型?
我有一个具有关联模型的存储,我需要将此关联模型的值包含到Sencha Touch中的selectfield组件中 这里是我的父模型:Javascript 如何在Sencha Touch的selectfield中实现嵌套和关联的json模型?,javascript,json,extjs,sencha-touch,Javascript,Json,Extjs,Sencha Touch,我有一个具有关联模型的存储,我需要将此关联模型的值包含到Sencha Touch中的selectfield组件中 这里是我的父模型: Ext.define('x.customer.model.CustomerModel', { extend: 'Ext.data.Model', requires:[ 'x.customer.model.CustomerTemplateModel' ], config: { useCache:
Ext.define('x.customer.model.CustomerModel', {
extend: 'Ext.data.Model',
requires:[
'x.customer.model.CustomerTemplateModel'
],
config: {
useCache: false,
idProperty: 'id',
fields: [
{
name: 'id',
type: 'string'
},
{
name: 'address',
type: 'string'
},
{
name: 'name',
type: 'string'
},
{
name: 'type',
type: 'int'
}
],
associations: [
{
type: 'hasMany',
associatedModel: 'Survey.customer.model.CustomerTemplateModel',
ownerModel: 'Survey.customer.model.CustomerModel',
associationKey: 'templates',
autoLoad: true,
name: 'templates'
}
]
}
});
儿童模型:
Ext.define('x.customer.model.CustomerTemplateModel', {
extend: 'Ext.data.Model',
requires:[],
config: {
useCache: false,
rootProperty: 'templates',
fields: [
{
name: 'text',
type: 'string'
},
{
name: 'value',
type: 'string'
}
]
}
});
商店:
requires: ['Survey.customer.model.CustomerModel'],
config: {
model: 'Survey.customer.model.CustomerModel',
proxy: {
type: 'ajax',
reader: {
type: 'json',
rootProperty: 'customers'
}
}
}
目前,json具有以下结构:
{
"id": "00000001",
"address": "Antsy Road",
"name": "asfas",
"phone": "55555",
"openSurveys": 7,
"templates": [
{
"text": "123",
"value": "Template 1"
}
],
"type": 1,
"withSurveys": true
},
如何实现selectfield中嵌套json的“模板”中包含的数据
一旦您的店铺加载完毕,并且如果您有一位客户,请提前向您表示感谢:
var templatesData = []; // What will be inserted to the ComboBox
for (var i=0; i < custommers[0].get('templates').length; i++) { // Running threw the values and populating templatesData
var templateModel = custommers[0].get('templates')[i];
var templateCombo = {
text: templateModel.data.text,
value: templateModel.data.value
};
templatesData.push(templateCombo);
}
// Setting the values to the combobox
Ext.getCmp('myCombo').setStore(Ext.create("Ext.data.Store", {
model: 'x.customer.model.CustomerTemplateModel',
data :templatesData
}));
var templatesData=[];//将在组合框中插入什么
对于(var i=0;i
这不是唯一的解决方案,您还可以创建一个新的store实例。下面是有关如何设置组合框的“store”属性的详细信息:在本例中,什么是“myObject”?“x.customer.model.CustomerModel”的实例“x.customer.model.CustomerModel”是您给父模型的名称,对吗?这个模型有很多“x.customer.model.CustomerTemplateModel”,所以你需要循环这个数据来填充你的组合:)我编辑了我的答案以更精确一些好的,我理解,但是如何获取这个父模型的数据?我想当你加载你的商店时?