Manish Pradhan
Manish Pradhan

Reputation: 1188

Extjs Model for nested JSON structure

Consider the following JSON structure

{
        "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

enter image description here

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

Upvotes: 0

Views: 1002

Answers (1)

norbeq
norbeq

Reputation: 3076

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(). The same for order item in order collection record : order_record.order_items().

Check this example on fiddle

Upvotes: 1

Related Questions