PDA

View Full Version : Override associations getter method.



incutonez
30 Jan 2014, 5:35 PM
I'd like to override the getter methods that come with the associations (hasMany, belongsTo, hasOne). It seems like a bit of magic is going on where the actual getters are created, and after looking over the code, I'm not really sure where to begin. I looked at the Ext.data.association.Association's constructor, but I wasn't able to deduce where the getters were created.

So let's say I have a hasMany relationship that looks like this:



hasMany: [
{associationKey: 'services', name: 'getServicesStore', model: 'Service'},
{associationKey: 'standards', name: 'getStandardsStore', model: 'Standard'}
]


When I call the getServicesStore and the getStandardsStore methods, I want to output something to the console, but I want both methods to output the same thing, so I want them to both still inherit from the same Associations class, but somewhere, override the Associations code.

I know my example may be a bit silly, but I've got other plans for this other than console logging. If anyone can provide any guidance as to where to start, I'd greatly appreciate it!

mitchellsimoens
6 Feb 2014, 12:39 PM
The getter methods are generated for you and not really something that can be easily overridden. For HasMany, it's generated in the createStore, for HasOne/BelongsTo is in the createGetter. Both these methods return a function which makes it difficult to override to add a log or custom code.

incutonez
6 Feb 2014, 3:51 PM
Thanks Mitchell, really appreciate the reply. That's what I had figured. My coworker and I ended up wrapping the name through a "private" method. If anyone's interested, here is our pattern:



hasOne: [
{associationKey: 'standard', getterName: '_getStandardModel', model: 'Standard'}
],
hasMany: [
{associationKey: 'services', name: '_getServicesStore', model: 'Services'}
],

/**
* Getter: returns the associations' Standard model
* @return {Standard} standardModel
*/
getStandardModel: function() {
var standardModel = this._getStandardModel();
if (!standardModel) {
this.logError('standardModel is undefined');
}
return standardModel ;
},

/**
* Getter: returns the associations' Services store
* @return {Services} servicesStore
*/
getServicesStore: function() {
var servicesStore = this._getServicesStore();
if (!servicesStore) {
this.logError('servicesStore is undefined');
}
return servicesStore ;
}