【问题标题】:Ext JS: Override Association stock getterExt JS:覆盖协会股票吸气剂
【发布时间】:2014-01-31 02:14:45
【问题描述】:

我想覆盖关联附带的 getter 方法(hasMany、belongsTo、hasOne)。似乎在创建实际 getter 的地方发生了一些魔术,在查看了代码之后,我不确定从哪里开始。我查看了Ext.data.association.Association's constructor,但无法推断出 getter 的创建位置。

假设我有一个类似这样的 hasMany 关系:

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

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

我知道我的例子可能有点傻,但除了控制台日志之外,我还有其他计划。如果有人能就从哪里开始提供任何指导,我将不胜感激!

来自Sencha forums的交叉帖子。

【问题讨论】:

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


    【解决方案1】:

    Mitchell Simoens 评论了我的 Sencha 主题:

    getter 方法是为你生成的,而不是真正的东西 可以很容易地被覆盖。对于 HasMany,它是在 用于 HasOne/BelongsTo 的 createStore 在 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 ;
    }
    

    我们认为这是我们使用的最佳实践。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      相关资源
      最近更新 更多