繁体   English   中英

如何在Sencha Touch中的selectfield中实现嵌套和关联的model-json?

[英]How to implemented a nested and associated model-json into selectfield in Sencha Touch?

我有一个具有关联模型的商店,我需要将此关联模型的值包括到Sencha Touch的selectfield组件中。

这是我的父模型:

    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 
}));

这不是唯一的解决方案,您也可以创建一个新的store实例。 这是有关如何为组合框设置“ store”属性的更多信息: http ://docs.sencha.com/extjs/4.1.3/#! /api/Ext.form.field.ComboBox-cfg-store

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM