代码之家  ›  专栏  ›  技术社区  ›  fguillen

EmberJS,DS.Model,如何知道特定属性是否脏?

  •  3
  • fguillen  · 技术社区  · 9 年前

    我可以 班长 财产 hasDirtyAttributes 以了解是否有任何属性是脏的。

    如何监视特定属性是否脏?

    类似于:

    attributeOneNeedSave: Ember.computed('attributeOne', function() {
      return this.get('dirtyAttributes.attributeOne');
    })
    
    1 回复  |  直到 9 年前
        1
  •  4
  •   fguillen    9 年前

    您可以使用 changedAttributes 方法来发现属性是否已更改。要将其转换为计算属性,只需在该属性更改时调用它。

    isNameDirty: Ember.computed('name', function() {
        const changedAttributes = this.changedAttributes();
        return !!changedAttributes.name;
    })
    

    此外,我不能100%确定Ember Data是否会从 changedAttributes 如果它变回原来的值。所以它 可以 有可能得到这样的东西:

    const changedAttributes = {
        name: ['Bob', 'Bob']
    };
    

    如果是这种情况,请检查是否相等。

    isNameDirty: Ember.computed('name', function() {
        const changedAttributes = this.changedAttributes();
        if (!changedAttributes.name) {
            return false;
        }
    
        return (changedAttributes.name[0] !== changedAttributes.name[1]);
    })
    
    推荐文章