0

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);
3
  • 1
    hello Can you provide store and item you are attaching it to? where is no problem with your code. it is working, you can check it on this fiddle fiddle.sencha.com/#view/editor&fiddle/2qqj Commented Mar 21, 2019 at 9:16
  • @LightNight Thanks for replying. I have updated my question to include the store and how I am loading the data. Commented Mar 21, 2019 at 14:18
  • @LightNight, I see the difference between your approach and mine. Can you tell me why your store model is set to "Product" instead of "User". I thought we were supposed to assign the root Model to the store in cases like this? Commented Mar 21, 2019 at 21:05

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You are right. This gives me what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.