当前位置: 首页 > 软件库 > 程序开发 > >

ember-diff-attrs

授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 董子航
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

ember-diff-attrs

This addon was spun out of a discussion on emberjs/rfcs#191 [Deprecate component lifecycle hook arguments].

ember-diff-attrs provides a dry way to track attribute changes using a component's didReceiveAttrs lifecycle hook.

PRs, RFCs and comments are welcome!

ember-did-change-attrs

@GavinJoyce and I (mostly Gavin) created an alternative version of this addon that offers a slightly cleaner API using a mixin instead of a decorator.

See: ember-did-change-attrs

Usage

Shorthand usage

import diffAttrs from 'ember-diff-attrs';

export default Ember.Component.extend({
  didReceiveAttrs: diffAttrs('email', 'isAdmin', function(changedAttrs, ...args) {
    this._super(...args);

    if(changedAttrs && changedAttrs.email) {
      let oldEmail = changedAttrs.email[0],
          newEmail = changedAttrs.email[1];
      // Do stuff
    }
  })
});

Some quick notes:

  • The function hook provided to diffAttrs will always be called, even when a tracked attr is not changed.
  • changedAttrs will be null on the first call.

Extended usage

import diffAttrs from 'ember-diff-attrs';

export default Ember.Component.extend({
  didReceiveAttrs: diffAttrs({
    keys: ['user', 'isAdmin'],
    isEqual(key, a, b) {
      if (key === 'user') {
        return (a && b) ? a.id === b.id : a === b;
      }
      return a === b;
    },
    hook(changedAttrs, ...args) {
      this._super(...args);

      if(changedAttrs && changedAttrs.user) {
        let oldUser = changedAttrs.user[0],
            newUser = changedAttrs.user[1];
        // Do stuff
      }
    }
  })
});

Design thoughts / rationales.

  • changedAttrs null on init -- It seems likely that some users will want an alternate behavior for init vs update. There is no loss of functionality by having changedAttrs null on init and it's easy to explain, nothing has actually changed yet.
  • changedAttrs structure -- I followed the precedence started by ember-data (model.changedAttributes()).

Outstanding Questions

Changed attrs format

I followed ember-data's precedence for representing old and new values (model.changedAttributes()). This format has always felt odd to me. I'm more than happy to discuss changing this.

didUpdateAttrs

Since this addon is implemented as a macro, it cannot easily utilize a component's init call to setup. Because of this, we are unable to determine what has changed the first time didUpdateAttrs is called.

Running tests

  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in "watch mode"
  • ember try:each – Runs the test suite against multiple Ember versions

Running the dummy application

For more information on using ember-cli, visit https://ember-cli.com/.

License

This project is licensed under the MIT License.

 相关资料
  • diff 用法 Usage: docker diff [OPTIONS] CONTAINER Inspect changes on a container's filesystem --help=false Print usage 例子 $ sudo docker diff b448f729a0b0 C /run A /run/secrets 总结 diff会列出3种容器内

  • 命名 git-diff - 显示提交,提交和工作树等之间的变化 概要 git diff [options] [<commit>] [--] [<path>…]git diff [options] --cached [<commit>] [--] [<path>…]git diff [options] <commit> <commit> [--] [<path>…]git diff [options

  • 传统 diff 算法 计算一棵树形结构转换成另一棵树形结构的最少操作,是一个复杂且值得研究的问题。传统 diff 算法通过循环递归对节点进行依次对比,效率低下,算法复杂度达到 O(n^3),其中 n 是树中节点的总数。O(n^3) 到底有多可怕,这意味着如果要展示1000个节点,就要依次执行上十亿次的比较。这种指数型的性能消耗对于前端渲染场景来说代价太高了!现今的 CPU 每秒钟能执行大约30亿条

  • Image-Diff是一款Uber开源的图片差异化处理工具,依赖ImageMagick。 安装 & 使用: npm install image-diff var imageDiff = require('image-diff');imageDiff({  actualImage: 'checkerboard.png',  expectedImage: 'white.png',  diffImage

  • Helm Diff Plugin This is a Helm plugin giving your a preview of what a helm upgrade would change.It basically generates a diff between the latest deployed version of a releaseand a helm upgrade --debu

  • hg-diff 是一个简单的 GUI 程序,用来浏览 mercurial 的修订版信息。可用于比较不同修订版之间的差异。