Search code examples
extjssencha-touchsencha-touch-2

Creating a model for two jsonarray


demoAlerts({
    itemstatus: [
        {
            item: "1",
            name: "apple",
            type: "fruit"
        },
        {
            item: "2",
            name: "pine",
            type: "fruit"
        }
    ],
    deliverystatus: [
        {
            name: "james",
            status: "yes"
        },
        {
            name: "thomas",
            status: "no"
        },

    ]
});

I have two array (itemstatus and deliverystatus), I need to create the model for this store. what I tried is

ParentModel:

Ext.define('test.model.ParentModel', {
    extend: 'Ext.data.Model',
    requires:['test.model.ItemModel','test.model.DeliveryModel'],
autoLoad: true,
    config : {
    fields : [ 'itemstatus', {
        name : 'demostatuslist',
        model : 'demoAlerts.model.ItemModel',
        mapping : 'itemstatus'
    },
     'portalstatus', {
            name : 'deliverystatus',
            model : 'test.model.DeliveryModel',
            mapping : ' deliverystatus'
        }]
    }

});

ItemModel

Ext.define('demoAlerts.model.ItemModel', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            { name: 'item', type: 'string' },
            { name: 'name', type: 'string' },
            { name: 'type', type: 'string' }

        ]
    }
});

DeliveryModel

Ext.define('demoAlerts.model.DeliveryModel', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'name', type: 'string' },
            { name: 'status', type: 'string' },


        ]
    }
});

Whether i properly configured the model. I am receiving the store as empty


Solution

  • Use Associations :) http://docs.sencha.com/touch/2.3.1/#!/api/Ext.data.association.Association

    In this case I would have a Model with 2 hasMany associations like this:

    Ext.define('demoAlerts.model.ContainerModel', {
        extend : 'Ext.data.Model',
        requires : [
            'demoAlerts.model.DeliveryModel',
            'demoAlerts.model.ItemModel'
        ],
        config : {
            associations: [
                {
                    type           : 'hasMany',
                    model          : 'demoAlerts.model.DeliveryModel',
                    associationKey : 'deliveryStatus',
                    name           : 'deliveryStatus',
                    autoLoad       : true // this is very important
                },
                {
                    type           : 'hasMany',
                    model          : 'demoAlerts.model.ItemModel',
                    associationKey : 'itemStatus',
                    name           : 'itemStatus',
                    autoLoad       : true // this is very important
                }
            ]
        }
    });
    

    Then use a store SomeStore binded to ContainerModel.

    IMPORTANT Each record in SomeStore will have deliveryStatusStore and itemStatusStore AUTOGENERATED.

    Read about associations.