无法从嵌套 JSON 获取'hasMany'数据



我有一个嵌套的JSON,我似乎无法提取嵌套数组。我不确定我的商店和模型设置是否正确。似乎没错。正如您在本文底部看到的那样,当我尝试取出嵌套部分时,它不起作用。

JSON数据:

{
"data":{
    "name":"John",
    "age":"23",
    "gender":"Male",
    "purchase":[
        {
            "item":"shirt",
            "qty":1,
            "price":10
        },
        {
            "item":"hat",
            "qty":2,
            "price":25
        },
        {
            "item":"pants",
            "qty":1,
            "price":15
        }
    ]
},
"success":true,
"errorMsg":""
}

商店:

Ext.define('Pvm.store.MyStore', {
    extend: 'Pvm.store.PvmBaseStore',

    requires: [
        'Pvm.model.Customer'
    ],

    model: 'Pvm.model.Customer',
    autoLoad: false
});

客户型号:

Ext.define('Pvm.model.Customer', {
    extend: 'Ext.data.Model',

    requires: [
        'Ext.data.Field',
        'Ext.data.proxy.Ajax',
        'Ext.data.reader.Json'
    ],

    fields: [{
        name: 'name',
        type: 'auto'
    }, {
        name: 'age',
        type: 'auto'
    }, {
        name: 'gender',
        type: 'auto'
    }],
    associations: {
        type: 'hasMany',
        model: 'Pvm.model.Purchase',
        name: 'purchase',
        associationKey: 'purchase'
    },

    proxy: {
        type: 'ajax',
        actionMethods: {
            create: 'POST',
            read: 'POST', // changed read's method to POST (from GET) so we get the parameters as form data (not in URL, for security reasons) 
            update: 'POST',
            destroy: 'POST'
        },
        url: '/pvmsvr/uiapi?cmd=readXml',
        reader: {
            type: 'json',
            rootProperty: 'data'
        }
    }
});

购买模式:

Ext.define('Pvm.model.Purchase', {
    extend: 'Ext.data.Model',

    requires: [
        'Ext.data.Field',
        'Ext.data.proxy.Ajax',
        'Ext.data.reader.Json'
    ],

    fields: [{
        name: 'item',
        type: 'auto'
    }, {
        name: 'qty',
        type: 'auto'
    }, {
        name: 'price',
        type: 'auto'
    }],

    associations: {
        type: 'belongsTo',
        model: 'Pvm.model.Customer'
    }

});

控制器代码:

onStoreLoaded: function(store, records, successful, eOpts) {
    console.log(store); // works
    console.log(records); // works
    console.log(records[0].get('name')); // works
    console.log(records[0].purchase(); // doesn't work; returns 'undefined'
}

我是白痴。我需要父模型的类名...

最新更新