简体   繁体   English

当嵌套在ExtJS 5.0.0中时如何读取引用的对象

[英]How to read the referenced object when it is nested in ExtJS 5.0.0

Below code works fine with the data as in ReadOrder.json (below), however how to read the associated object when it is nested inside another object as in ReadOrderNested.json(below, within 'collection'). 下面的代码可以像ReadOrder.json(在下面)中那样处理数据,但是,当关联对象嵌套在另一个对象中时,如ReadOrderNested.json(在“集合”中)一样,如何读取关联的对象。

Question is more specifically can we use a mapping property or proxy's reader config with rootProperty (tried this approach with no luck) 问题更具体地讲,我们是否可以将映射属性或代理的读取器配置与rootProperty一起使用(尝试这种方法没有任何运气)

Sencha fiddle : https://fiddle.sencha.com/#fiddle/9fb Sencha小提琴: https : //fiddle.sencha.com/#fiddle/9fb

Extjs version : 5.0.0 Extjs版本:5.0.0

    //Base Model
Ext.define('MyApp.model.Base', {
    extend: 'Ext.data.Model',


    fields: [{
        name: 'id',
        type: 'int'
    }],


    schema: {
        namespace: 'MyApp.model'
    }
});




//Order Model
Ext.define('MyApp.model.Order', {
    extend: 'MyApp.model.Base',


    fields: [{
        name: 'customer',
        type: 'string'
    }, {
        name: 'paymentStatus',
        type: 'string'
    }]
});




//PaymentDetail Model
Ext.define('MyApp.model.PaymentDetail', {
    extend: 'MyApp.model.Base',
    fields: [{
        name: 'orderId',
        reference: 'Order'
    }, {
        name: 'cardNumber',
        type: 'string'
    }, {
        name: 'status',
        type: 'string'
    }]
});




Ext.define('MyApp.store.OrderStore', {
    extend: 'Ext.data.Store',
    model: 'MyApp.model.Order',
    proxy: {
        type: "rest",
        url: 'Order.json',
        appendId: false,
        api: {
            create: undefined,
            read: 'ReadOrder.json',
            update: 'UpdateOrder.json',
            destroy: undefined
        },
        reader: {
            type: 'json',
            rootProperty: 'order'
        },
        writer: {
            writeAllFields: true,
            allDataOptions: {
                persist: true,
                associated: true
            }
        }
    },
});








Ext.application({
    name: 'MyApp',


    launch: function() {


        var orderStore = Ext.create('MyApp.store.OrderStore');
        orderStore.load({
            callback: function(records) {
                var order = this.first();
                debugger;
                var paymentDetailList = order.paymentDetails();


                paymentDetailList.each(function(paymentDetail) {
                    //Print initial values of payment detail
                    console.log(paymentDetail.get('cardNumber'));
                    console.log(paymentDetail.get('status'));


                })


            }
        });


    }
});

Data : ReadOrder.json 数据:ReadOrder.json

{    "success": true,
    "order": [{
        "id": 1,
        "customer": "Philip J. Fry",
        "paymentStatus": "AWAIT_AUTH",
        "paymentDetails": [{
                orderId : 1,
                "cardNumber": "4111111111",
                "status": 'CREATED'
            }, {
                orderId : 1,
                "cardNumber": "4222222222",
                "status": "CREATED"
            }]
    }]
}

How to read with this data when the associated object is nested inside 'collection', ReadOrderNested.json: 当关联对象嵌套在“集合” ReadOrderNested.json中时,如何读取此数据:

{    "success": true,
    "order": [{
        "id": 1,
        "customer": "Philip J. Fry",
        "paymentStatus": "AWAIT_AUTH",
        "paymentDetails": {
            "collection" : [{
                orderId : 1,
                "cardNumber": "4111111111",
                "status": 'CREATED'
            }, {
                orderId : 1,
                "cardNumber": "4222222222",
                "status": "CREATED"
            }]}


    }]
}

I am using ExtJS 4, dunno whether there is a difference. 我正在使用ExtJS 4,不知道是否有区别。 I am using a model with fields like this: 我正在使用具有以下字段的模型:

fields: [{
    name: 'id',
    type: 'int'
},{
    name: 'paymentDetails'
}],

and when loading one model into a form 当将一个模型加载到表单中时

form.load(record);
Ext.getStore("paymentDetailsStore").removeAll();
Ext.getStore("paymentDetailsStore").loadRawData(record.get("paymentDetails"));

with paymentDetailsStore bound to a grid which is in the same window as the form. paymentDetailsStore绑定到与paymentDetailsStore位于同一窗口中的网格。

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

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