Ext JS:覆盖Association stock getter

时间:2014-01-31 02:14:46

标签: extjs model associations has-many extjs4.2

我想覆盖关联附带的getter方法(hasMany,belongsTo,hasOne)。在创建实际的getter的地方似乎有点神奇,在查看代码后,我不确定从哪里开始。我查看了Ext.data.association.Association's constructor,但我无法推断出吸气剂的创建位置。

所以,假设我有一个看起来像这样的hasMany关系:

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

当我调用getServicesStoregetStandardsStore方法时,我想输出一些东西到控制台,但我希望两种方法都输出相同的东西,所以我希望它们两者仍然继承自相同的Associations类,但在某处,覆盖关联代码。

我知道我的例子可能有点傻,但除了控制台记录之外,我还有其他计划。如果有人可以提供任何指导,我会非常感激!

来自Sencha forums的交叉发布。

1 个答案:

答案 0 :(得分:1)

Mitchell Simoens在我的Sencha帖子中评论了这个:

  

getter方法是为你生成的,而不是真正的东西   可以很容易地被覆盖。对于HasMany,它是在   createStore,用于HasOne / BelongsTo在createGetter中。这两个   方法返回一个函数,这使得很难覆盖添加   日志或自定义代码。

所以我的同事和我创建了“私人”的getter名称,并且仅在该模型中使用它们,如下所示:

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

我们认为这是我们使用的最佳做法。

相关问题