Results 1 to 5 of 5

Thread: Model associations and bindings

  1. #1
    Sencha Premium Member
    Join Date
    Feb 2015
    Posts
    32

    Question Model associations and bindings

    Hello,

    I struggle at the moment a bit using model association and binding.

    I try to create a view with a binded model. The view also has a grid with some sub records. My store has all the data like:

    Code:
    [
            {
                id: 1,
                "name": "MyName",
                "items" : [
                    {"description": "My Description","amount": 3,id: 2}
                ]
            },
            {
                "id": 2,
                "name": "OtherName",
                "items": [
                    {"description": "Other Description","amount": 2, id: 1} 
                ]
            }
        ]
    I try to bind this items via

    Code:
          {
                itemId: 'gridId',
                xtype: 'grid',
                bind: {
                    data: '{theOrder.data.items}'
                },
                columns: [
                    {header: 'description', dataIndex: 'description', flex:1, 
                         editor: {
                             xtype: 'textfield',
                             allowBlank: false
                         }
                    },
                    {header: 'amount', dataIndex: 'amount', flex:1, 
                         editor: {
                            xtype: 'textfield',
                            allowBlank: false
                        }
                    }
                ],
                
                plugins: [
                    {ptype: 'cellediting', clicksToEdit: 1}
                ]
            }
    but it doesn't work. My target is to save the parent record with his child records in one request (transaction safety). Working with a blank store, modifying some child records and save afterwards does work. But is there a best practice for binding child records and save them after editing with a single sync on the parent store?

    Feel free to use my fiddle to experiment:

    https://fiddle.sencha.com/#fiddle/mav

    kind regards

  2. #2
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    You have no associations defined between Order and Items. See this article in docs: http://docs.sencha.com/extjs/5.1/cor...l#Associations
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  3. #3
    Sencha Premium Member
    Join Date
    Feb 2015
    Posts
    32

    Default

    Hey,

    thank you for your fast response.

    I have

    Code:
    Ext.define('Item', {
        extend: 'Ext.data.Model',
    
    
        fields: [
             {name:'description', type: 'string'},
             {name:'amount', type: 'int'}
        ]
        
    });
            
    Ext.define('Order', {
        extend: 'Ext.data.Model',
        idProperty: 'id',
        
        
        fields: [
             {name:'id', type: 'int'},
             {name:'name', type: 'string'},
            {name: 'orderItem', reference: 'Item'}
           
        ]
        
    });
    My input data looks like:

    Code:
    {
        "success": true,
        "data": [
            {
                id: 1,
                "name": "MyName",
                "orderItem" : [
                    {"description": "My Description","amount": 3,id: 2}
                ]
            },
            {
                "id": 2,
                "name": "OtherName",
                "orderItem": [
                    {"description": "Other Description","amount": 2, id: 1} 
                ]
            }
        ]
    }
    I dont get a valid reference store

    https://fiddle.sencha.com/#fiddle/mav

  4. #4
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    You have it mixed up. reference must be set on many side so Item must have an orderId and that must have reference set to order. Then, in JSON you must have member "items" in order record. See also http://docs.sencha.com/extjs/5.1/cor...ng_Nested_Data
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  5. #5
    Sencha Premium Member
    Join Date
    Feb 2015
    Posts
    32

    Default

    Ok, I get some valid data via

    Code:
    store.getAt(0).getAssociatedData();
    My data:

    Code:
    {
        "success": true,
        "data": [
            {
                id: 1,
                "name": "MyName",
                "orderItem" : [
                    {"description": "My Description","amount": 3,id: 2, orderId: 1}
                ]
            },
            {
                "id": 2,
                "name": "OtherName",
                "orderItem": [
                    {"description": "Other Description","amount": 2, id: 1, orderId: 2} 
                ]
            }
        ]
    }
    notice: now orderId is duplicated content. Its nested so i think set this order id should be optional.

    My model:

    Code:
    Ext.define('Item', {
        extend: 'Ext.data.Model',
    
        fields: [
             {name:'description', type: 'string'},
             {name:'amount', type: 'int'},
             {name: 'orderId', reference: 'Order'}
        ]
        
    });
            
    Ext.define('Order', {
        extend: 'Ext.data.Model',
        idProperty: 'id',
        
        
        fields: [
             {name:'id', type: 'int'},
             {name:'name', type: 'string'},
            {name: 'orderItem', reference: 'Item'}
           
        ]
        
    });
    I see a hidden store for associated data in my console. Is this associated data store legit? I like to bind associated data to the grid within a store. The target is to manipulate this data and save this nested data with a singel sync / save. This would work if the data and the associated data store is always in sync or is syncable.

Similar Threads

  1. [INFOREQ] Model bindings and model validations
    By sebastian2015 in forum Ext 5: Bugs
    Replies: 7
    Last Post: 5 May 2015, 6:54 AM
  2. Replies: 3
    Last Post: 18 Nov 2014, 3:20 PM
  3. [DUP] [2.1] Due to the Model.cache, associations aren't updated when reloading a model
    By Steffen Hiller in forum Sencha Touch 2.x: Bugs
    Replies: 2
    Last Post: 22 Aug 2012, 7:42 AM
  4. How to fix Ext.List "update" bindings when store has associations
    By luismerino in forum Sencha Touch 2.x: Q&A
    Replies: 4
    Last Post: 6 Mar 2012, 2:19 PM
  5. Replies: 1
    Last Post: 13 Feb 2012, 12:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •