Results 1 to 9 of 9

Thread: HasOne association from nested json?

  1. #1
    Sencha User
    Join Date
    Oct 2012
    Posts
    28

    Default HasOne association from nested json?

    Hello,
    there is possible to fetch whole associated from single json with hasMany association, but I can not achieve this with hasOne.

    I need like this:
    Code:
    associations: [
            {type: 'hasOne', model: 'myApp.model.Record', name: 'myRecord'}
        ]
    Code:
    from json:
    {
      "id": 1,
      "name": "Some stuff",
      "myRecord": {
          ...
      }
       .....
    }

  2. #2
    Sencha User kevin.chen's Avatar
    Join Date
    Sep 2012
    Location
    Redwood City, CA
    Posts
    242
    Answers
    12

    Default

    you can do it, you just don't know how to access it, if you give instanceName in your association, then you can access the nested data by parent.instanceName

  3. #3

  4. #4
    Sencha User kevin.chen's Avatar
    Join Date
    Sep 2012
    Location
    Redwood City, CA
    Posts
    242
    Answers
    12

    Default

    well, you can try assocationKey since 'instanceName' is not documented.

    for me, I have no problem to use instanceName

    you can check out hasOne constructor, it is there.

    Code:
        constructor: function(config) {
            this.callParent(arguments);
    
    
            var me             = this,
                ownerProto     = me.ownerModel.prototype,
                associatedName = me.associatedName,
                getterName     = me.getterName || 'get' + associatedName,
                setterName     = me.setterName || 'set' + associatedName;
    
    
            Ext.applyIf(me, {
                name        : associatedName,
                foreignKey  : associatedName.toLowerCase() + "_id",
                instanceName: associatedName + 'HasOneInstance',
                associationKey: associatedName.toLowerCase()
            });
    
    
            ownerProto[getterName] = me.createGetter();
            ownerProto[setterName] = me.createSetter();
        }

  5. #5
    Sencha User kevin.chen's Avatar
    Join Date
    Sep 2012
    Location
    Redwood City, CA
    Posts
    242
    Answers
    12

    Default

    regarding associated model , I have one example I did for xml. you can take a look as reference

    Code:
    Ext.define('app.model.MyModel',{
        extend: 'Ext.data.Model',
        fields: [
            { name:   'myId',    mapping: '@id',    type: 'string'}
        ],
        idProperty: 'myId',
        associations: [
                         {
                            type: 'hasMany',
                            model: 'app.model.Person',
                            name: 'persons',
                            reader: {
                                    type: 'xml',
                                    root: 'persons',
                                    record: '>person'
                            }
                         },
                         {
                            type: 'hasMany',
                            model: 'app.model.Product',
                            name: 'products',
                            reader: {
                                type: 'xml',
                                root: 'products',
                                record: '>product'
                            }
                         }
    
    
        ]
    
    
    });
    
    
    
    
    Ext.define('app.model.Person', {
        extend: 'Ext.data.Model',
        fields: [
            { name:   'id',    mapping: '@id',    type: 'string'},
            { name:  'name',   mapping: '>name',  type: 'string'},
            { name:  'gender', mapping: '>gender',type: 'string'}
        ],
        associations: [{ type: 'hasOne',
                         model: 'app.model.Address',
                         associationKey: 'address',
                         instanceName: 'address1',
                         name: 'address',
                         reader: {
                                    type: 'xml',
                                    root: 'address',
                                    record: 'address'
                         }
                        }]
    });
    
    
    
    
    Ext.define('app.model.Address', {
        extend: 'Ext.data.Model',
        fields: [
            { name:  'street',  mapping: '>street',    type: 'string'},
            { name:  'city',    mapping: '>city',      type: 'string'},
            { name:  'state',   mapping: '>state',     type: 'string'},
            { name:  'country', mapping: '>country',   type: 'string'}
        ]
    });
    
    
    
    
    
    
    Ext.define('app.model.Product', {
        extend: 'Ext.data.Model',
        fields: [
            { name:   'id',    mapping: '@id',    type: 'string'},
            { name:  'name',   mapping: '>name',  type: 'string'}
        ],
        associations: [
                        {
                            type: 'hasMany',
                            model: 'app.model.User',
                            name: 'users',
                            reader: {
                                type: 'xml',
                                root: 'users',
                                record: '>user'
                            }
                        }
        ]
    });
    
    
    
    
    Ext.define('app.model.User', {
        extend: 'Ext.data.Model',
        fields: [
            { name:   'id',    mapping: '>id',    type: 'string'},
            { name:  'name',   mapping: '>name',  type: 'string'}
        ]
    });

  6. #6
    Sencha User kevin.chen's Avatar
    Join Date
    Sep 2012
    Location
    Redwood City, CA
    Posts
    242
    Answers
    12

    Default

    here is the test data

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <data>
    	<persons>
    		<person id="uid_01">
    			<name>Jhon</name>
    			<gender>Male</gender>
    			<address>
    				<street>Timberlake</street>
    				<city>Croydon</city>
    				<state>London</state>
    				<country>England</country>
    			</address>
    		</person>
    		<person id="uid_02">
    			<name>Leon</name>
    			<gender>Male</gender>
    			<address>
    				<street>306 Division Street</street>
    				<city>King City</city>
    				<state>California</state>
    				<country>United States</country>
    			</address>
    		</person>
    	</persons>
    	<products>
    		<product id="cos_timm001">
    			<name>Trimmer</name>
    			<users>
    				<user>
    					<id>uid_01</id>
    					<name>Jhon</name>
    				</user>
    				<user>
    					<id>uid_0901</id>
    					<name>Tom</name>
    				</user>
    			</users>
    		</product>
    		<product id="cos_cell001">
    			<name>IPhone5</name>
    			<users>
    				<user>
    					<id>uid_02</id>
    					<name>Leon</name>
    				</user>				
    			</users>
    		</product>
    	</products>
    </data>

  7. #7
    Sencha User kevin.chen's Avatar
    Join Date
    Sep 2012
    Location
    Redwood City, CA
    Posts
    242
    Answers
    12

    Default

    you can access first person address by following (instanceName=address1)

    Code:
    Ext.getStore('MyStore').data.items[0].persons().data.items[0].address1
    access second person's address by following(instancName=address1)

    Code:
    Ext.getStore('MyStore').data.items[0].persons().data.items[1].address1

  8. #8
    Ext JS Premium Member
    Join Date
    Jul 2011
    Posts
    46
    Answers
    1

    Default

    The above did not work for me in ExtJS 4.1.2

    I think we should file a bug. HasOne association is not able to read nested data and always tries to request for the record from the server.

  9. #9
    Sencha Premium Member
    Join Date
    Nov 2013
    Location
    Tolmin, Slovenia
    Posts
    43
    Answers
    1

    Default

    It seems that the same problem (hasOne wants to read from the server for nested resource) is present in ExtJS 4.2.2 as well.

Posting Permissions

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