문제

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!

Cross-post from the Sencha forums.

도움이 되었습니까?

해결책

Mitchell Simoens commented on my Sencha thread with this:

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.

So my coworker and I created "private" getter names, and used them only in that model, like this:

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 ;
}

We figured this was the best practice for our use.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top