Results 1 to 3 of 3

Thread: Nested objects in a model?

  1. #1

    Default Nested objects in a model?

    I receive the below from an api call. Is it possible to use nested objects in a model? If so, how would I use the regModel to register the below hierarchy?

    Thanks.

    Code:
    {
    "Store": {
    	"id":"1721",
    	"name":"The Widget Store",
    	"address_1":"91 Widget Rd",
    	"address_2":"-",
    	"marker":"default_marker.png",
    	"lat":"-33.8868",
    	"lng":"151.177"
    	},
    	"Suburb": {
    		"name":"Widget Land"
    	}
    }

    Code:
    Ext.regModel('Store', {
    	fields: ['id', 'name', 'address_1', 'address_2', 'marker', 'lat', 'lng', 'suburb']
    });

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,448
    Answers
    3997

    Default

    You won't be able to easily. You have to go to the reader, use the createAccessor method like it does for the total property in order to get that.

    Code:
    reader.getSuburb = reader.createAccessor('Suburb');
    Now the reader will have a getSuburb method that you can execute passing the raw data.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  3. #3
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    You could jump through some hoops with mappings. It's a bit unpleasant though.

    Code:
    Ext.define('MyModel', {
        extend: 'Ext.data.Model',
    
        fields: [
            {name: 'id', mapping: 'Store.id'},
            {name: 'name', mapping: 'Store.name'},
            ...
            {name: 'suburb', mapping: 'Suburb.name'}
        ]
    });
    Ext.regModel is deprecated, use Ext.define. Note also that models are just classes and live in the global namespace, so you should really namespace them just like you would any other class.

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
  •