1. #1
    Sencha User
    Join Date
    Mar 2007
    Posts
    416
    Vote Rating
    18
    Answers
    5
    jweber will become famous soon enough

      0  

    Default Unanswered: Need help with hasOne association

    Unanswered: Need help with hasOne association


    I'm trying to create a hasOne association on a model, and provide the associated data along with the parent model's data.

    But I'm not sure how to configure the association, or how to access the associated data from an instance of the parent model. Could someone point me in the right direction?

    As an example, with data like this:
    Code:
    {
      firstName: 'Joe',
      address: {
         street: 'Avenue L',
         city: 'Brooklyn'
      }
    }
    And models like this:
    Code:
    Ext.define('app.model.Person', {
    	extend: 'Ext.data.Model',
    	config: {
    		fields: [
    			{name: "firstName", type: "string"}
    		],
    		hasOne: {
    			// this is the part I'm not sure about
    			model: 'app.model.Address',
    			name: 'address'
    		},
    		proxy: {
    			type: 'ajax',
    			...
    			reader: {
    				type: 'json'
    			}
    		}
    	}
    });
    
    Ext.define('app.model.Address', {
    	extend: 'Ext.data.Model',
    	config: {
    		fields: [
    			{name: "street", type: "string"},
    			{name: "city", type: "string"}
    		]
    	}
    });
    What would I put inside hasOne? And how would I access a Person's associated Address?

    The intro to the API docs for HasOne is a little confusing. It seems to indicate that if X hasOne Y, the hasOne association should be configured on Y's model, not X's. Is this a typo? Also, the config names on the Ext.data.association.* classes (e.g. "associatedModel") are different from the ones used in almost every example (e.g. "model"). So it's not clear what maps to what.

  2. #2
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Vote Rating
    6
    Answers
    93
    rdougan will become famous soon enough

      0  

    Default


    Yes, the documentation for that is wrong and will be updated in the next release. Sorry about that!

    Your hasOne association should be on the person. The name field will automatically map to the field ont he record (so 'address' would use the 'address' field and create the 'getAddress' getter). If for some reason you want these to be different, you can use 'associationKey' to set the actual record field to be used.

    Is your code working?
    Sencha Inc.
    Robert Dougan - @rdougan
    Sencha Touch 2 and Ext JS 4 Core Team Member, SASS/Theming Wizard.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    416
    Vote Rating
    18
    Answers
    5
    jweber will become famous soon enough

      0  

    Default


    It didn't work as posted, although I've managed to cobble together something that works.
    • The "name" config doesn't seem to do anything with HasOne. It seems like this only works with HasMany.
    • Instead, I replaced it with two separate configs: "associationKey" for the field containing the associated data, and "getterName" for the getter function name:
      Code:
      hasOne: {
      	model: 'app.model.Address',
      	associationKey: 'address',
      	getterName: 'getAddress'
      }
    • In addition, I had to specify a proxy on the Address model. This seems strange, since I might want to use this model in other contexts:
      Code:
      proxy: {
      	type: 'memory'
      }

  4. #4
    Sencha User Kikketer's Avatar
    Join Date
    Apr 2012
    Location
    Wausau, WI
    Posts
    135
    Vote Rating
    8
    Answers
    6
    Kikketer will become famous soon enough

      0  

    Default


    The documentation is still quite wrong, without even testing you can see there are issues.
    http://docs.sencha.com/touch/2.3.1/#...ciation.HasOne

    The example at the top has a simple:
    Code:
    associations: { type: 'hasOne', model: 'Address' }
    "model" is not a configuration option in the lower documentation. Also the two required config options are not even addressed in this example.

    Time to trial and error this thing. Anyone come up with the "Sencha Wiki" yet that actually has up to date documentation?

  5. #5
    Ext JS Premium Member
    Join Date
    Dec 2010
    Location
    Orlando, FL
    Posts
    21
    Vote Rating
    1
    Answers
    1
    mangotang is on a distinguished road

      0  

    Default Any good documentation for hasOne, hasMany and belongsTo

    Any good documentation for hasOne, hasMany and belongsTo


    Any good documentation for hasOne, hasMany and belongsTo properties of Models?

  6. #6
    Sencha User
    Join Date
    Sep 2014
    Posts
    13
    Vote Rating
    -2
    MrFox has a little shameless behaviour in the past

      0  

    Default


    @jweber thanks for trying, but I can't get this to work. I want to specify a bunch of status objects once and then load them for every record and it's statusId. Seems I'll have to load em manually.