Rules for HasMany Associations in ExtJS
- Always put your Proxies in your Models, not your Stores, unless you have a very good reason not to *
- Always require your child models if using them in hasMany relationships. **
- Always use foreignKey if you want to load the children at will
- Always use associationKey if you return the children in the same response as the parent
- You can use both foreignKey and associationKey if you like
- Always name your hasMany relationships
- Always use fully qualified model names in your hasMany relationship
- Consider giving the reader root a meaningful name (other than "data")
- The child model does not need a belongsTo relationship for the hasMany to work
Rules for HasOne and BelongsTo Associations in ExtJS:
Put the proxy in the model, unless you have a very good reason not to [1]
• Always use fully qualified model name
• Always set the getterName
• Always set the setterName
• Always set the associationKey, if the foreign object is returned in the same response as this object
• Always set the foreignKey, if you want to load the foreign object at will
• Consider changing the instanceName to something shorter
• The getter behaves differently depending on whether the foreign object is loaded or not. If its loaded, the foreign object is returned. Otherwise, you need to pass in a callback to get it.
• You should set the name property if you plan to override this association.
• You do not need a belongsTo relationship for a hasMany to work
• Set the primaryKey property if the id field of the parent model is not "id"
• Sometimes you need to use uses or requires for the belongsTo association. Watch out for circular references though.
• Calling setter() function does not seem to set the instance. Set object.belongsToInstance = obj if calling the setter().
HasMany Example :
Ext.define('Assoc.model.Contact', { extend:'Ext.data.Model', requires:[ 'Assoc.model.PhoneNumber' /* rule 2 */ ], fields:[ 'name' /* id field is inherited from Ext.data.Model */ ], hasMany:[ { foreignKey: 'contact_id', /* rule 3, 5 */ associationKey: 'phoneNumbers', /* rule 4, 5 */ name: 'phoneNumbers', /* rule 6 */ model: 'Assoc.model.PhoneNumber' /* rule 7 */
} ], proxy:{ /* rule 1 */ type: 'ajax', url: 'assoc/data/contacts.json', reader: { type: 'json', root: 'contacts' /* rule 8 */ } } }); // example usage: var c = new Assoc.model.Contact({id:99}); /* * assuming that PhoneNumber.proxy is AJAX and has a url set to 'phonenumbers', the below function would make an http request like this: * /phonenumbers?page=1&start=0&limit=25&filter=[{"property":"contact_id","value":"99"}] * ie, it would make a request for all phone numbers, but ask the server to filter them by the contact_id of the Contact, which is 99 in this case */ c.phoneNumbers().load();
* The store will inherit its model's proxy, and you can always override it
** To make it easy, and avoid potential circular references, you can require them in app.js .
BelongsTo Example:
Ext.define('Assoc.model.PhoneNumber', { extend:'Ext.data.Model', fields:[ 'number', 'contact_id' ], belongsTo:[ { name:'contact', instanceName:'contact', model:'Assoc.model.Contact', getterName:'getContact', setterName:'setContact', associationKey:'contacts', foreignKey:'contact_id' } ], proxy:{ type:'ajax', url:'assoc/data/phone-numbers.json', reader:{ type:'json', root:'phoneNumbers' } } });
Note about associationKey:
The mapping and associationKey configs do the same thing but do different things and are not going to work together.I personally would change the server to make it easy on the client to get this data. Or you could extend the reader and handle the raw data when the response comes in to unnest the data.
Eg:
{name: 'jmeterOverrideEnable', mapping: 'jmeterOptions.overrideOptions.enable', type: 'boolean'}, {name: 'jmeterOverrideThreads', mapping: 'jmeterOptions.overrideOptions.threads', type: 'number'}, {name: 'jmeterOverrideRampUpTime', mapping: 'jmeterOptions.overrideOptions.rampUpTime', type: 'number'}, {name: 'jmeterOverrideIterations', mapping: 'jmeterOptions.overrideOptions.iterations', type: 'number'}, {name: 'jmeterOverrideDuration', mapping: 'jmeterOptions.overrideOptions.duration', type: 'number'}, {name: 'jmeterOverrideEnginesVsUsers', mapping: 'jmeterOptions.overrideOptions.engines_vs_users', type: 'number'}
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
ReplyDeleteKindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Online Training from India . or learn thru JavaScript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry.
ES6 Training in Chennai