简体   繁体   English

用于嵌套JSON结构的Extjs模型

[英]Extjs Model for nested JSON structure

Consider the following JSON structure 考虑以下JSON结构

{
        "id": 123,
        "name": "Ed",
        "orders": [
            {
                "id": 50,
                "total": 100,
                "order_items": [
                    {
                        "id": 20,
                        "price": 40,
                        "quantity": 2,
                        "product": {
                            "id": 1000,
                            "name": "MacBook Pro"
                        }
                    },
                    {
                        "id": 21,
                        "price": 20,
                        "quantity": 3,
                        "product": {
                            "id": 1001,
                            "name": "iPhone"
                        }
                    }
                ]
            }
        ]
    }

Here are my models 这是我的模特

Ext.define("User", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'name'
    ],

    hasMany: {model: 'Order', name: 'orders', associationKey: 'orders'}
});

Ext.define("Order", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'total'
    ],

    hasMany  : {model: 'OrderItem', name: 'orderItems', associationKey: 'order_items'}
});

Ext.define("OrderItem", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'price', 'quantity'
    ],
    hasOne : {
        model: 'Product', 
        name: 'product', 
        associationKey: 'product'
    }
});

Ext.define("Product", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'name'
    ]
});

When I load the data in my store and then check the store record I see this 当我在商店中加载数据,然后检查商店记录时,我看到了

在此处输入图片说明

I do not get the Orders and stuff inside it. 我没有订单和里面的东西。 There must be something wrong with the way I have defined the models but I cant seem to figure it out. 我定义模型的方式肯定有问题,但我似乎无法弄清楚。 Thanks in advance. 提前致谢。

Update Here is my store and how I am loading the data 更新这是我的商店以及我如何加载数据

Ext.define('Company.store.TestOrders', {
    extend: 'Ext.data.Store',
    alias: 'store.testorders',
    model: 'User',
    data:[
    {
        "id": 123,
        "name": "Ed",
        "orders": [
            {
                "id": 50,
                "total": 100,
                "order_items": [
                    {
                        "id": 20,
                        "price": 40,
                        "quantity": 2,
                        "product": {
                            "id": 1000,
                            "name": "MacBook Pro"
                        }
                    },
                    {
                        "id": 21,
                        "price": 20,
                        "quantity": 3,
                        "product": {
                            "id": 1001,
                            "name": "iPhone"
                        }
                    }
                ]
            }
        ]
    }],
    storeId: 'TestOrders',
    proxy: {
        type: 'memory'
    }
});

Then later I am looking at the data by using 然后稍后我通过使用

Ext.getStores('TestOrders').getAt(0);

Maybe are you looking for method to get orders and order items from User store? 也许您正在寻找从用户商店获取订单和订购商品的方法?

You can get orders collection from user record using method record.orders() . 您可以使用record.orders()方法从用户记录中获取订单集合。 The same for order item in order collection record : order_record.order_items() . 订单收集记录中的订单商品相同: order_record.order_items()

Check this example on fiddle 在小提琴上检查这个例子

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

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