您所在的位置:小祥子 » 编程 » JavaScript » 正文

EmberJs之数组绑定@each&[]

时间:2015-08-18 编辑:史蒂芬King 来源:CnBlogs

写在前面

  好长时间没有写博客了,昨天花了些时间又整理了下之前发布过的《Ember.JS之computed Property》文章,并创建了一个测试代码库,花了些时间,希望能使用测试代码的方式,来演示如何使用Ember.js同时能避免升级Ember版本后,一些功能上的变化所带来的隐含Bug。

 

  如果大家对Ember.js有兴趣想一起研究的话,欢迎大家一起维护测试代码库  :)

  本文主要是针对Ember.Array中的[]和@each,数组方法进行详细分析。

 

文章索引

JS前端框架之Ember.js系列

 

一、如何使用[] & @each

  Ember中二者均可用于计算属性的绑定,使用十分方便,可以像如下方式定义依赖关系:

 totalArea: Ember.computed('sizeArray.[]', function () {
       return this.get('sizeArray').reduce(function (prev, cur) {
         return prev + Ember.get(cur, 'height') * Ember.get(cur, 'width');
       }, 0);
 }),

  或者:

 totalArea: Ember.computed(‘sizeArray.@each', function () {
       return this.get('sizeArray').reduce(function (prev, cur) {
         return prev + Ember.get(cur, 'height') * Ember.get(cur, 'width');
       }, 0);
 }),

  这样就定义了一个依赖于数组sizeArray的计算属性,建立一个绑定关系,只要sizeArray发生变化,totalArea就会被重新计算,使用十分简单,只要在function前面罗列出依赖的属性即可。虽然使用简单,但是在使用上还是有些细节的,下一节将用测试代码来讲解使用上的细节。

注:Ember计算属性有多种写法,这里给出了Ember推荐写法,更多具体细节请参考文章《Ember.js之computed Property-1. What is CP》

 

二、Array.[] & Array.@each特性总结

(Ember v1.13.7) 

1. Array.@each.property 特性

当CP属性依赖于.property('columns.@each.isLoaded')时:

  - columns里面任何一个元素的isLoaded属性发生变化。

  - columns增加元素或者删除子元素。

  - columns数组本身被重新赋值。

  - 不能依赖@each.owner.@each.name 。

 test('computed property depend on @each.height', function (assert) {
   var Size = Ember.Object.extend({height: 0, width: 0});
   var rectangle = Ember.Object.extend({
     totalArea: Ember.computed('sizeArray.@each.height', function () {
       return this.get('sizeArray').reduce(function (prev, cur) {
         return prev + Ember.get(cur, 'height') * Ember.get(cur, 'width');
       }, 0);
     }),
     sizeArray: [
       Size.create({height: 10, width: 10}),
       Size.create({height: 10, width: 10})
     ]
   }).create();
   var sizeArray = rectangle.get('sizeArray');
 
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200");
 
   sizeArray.pushObject(Size.create({height: 10, width: 10}));
   assert.equal(rectangle.get('totalArea'), 300, "the total area should be 300 after added");
 
   sizeArray.removeAt(0);
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200 after removed");
 
   sizeArray[0].set('height', 20);
   assert.equal(rectangle.get('totalArea'), 300, "the total area should be 300 after 'height' changed");
 
   sizeArray[0].set('width', 20);
   assert.equal(rectangle.get('totalArea'), 300, "the total area should not be changed after 'width' changed");
 
   sizeArray.clear();
   assert.equal(rectangle.get('totalArea'), 0, "the total area should be 0 after reset rectangle.sizeArray");
 });

[参考代码](https://github.com/Cuiyansong/ember-table-learnning/blob/master/ember-test/tests/unit/computed-property-test.js#L164-Lundefined)

 

2. Array.@each特性

当CP属性依赖于.property('columns.@each')时:

  - columns增加或删除元素。

  - columns自身被替换或重新赋值。

 test('computed property depend on @each', function (assert) {
   var Size = Ember.Object.extend({height: 0, width: 0});
   var rectangle = Ember.Object.extend({
     totalArea: Ember.computed('sizeArray.@each', function () {
       return this.get('sizeArray').reduce(function (prev, cur) {
         return prev + Ember.get(cur, 'height') * Ember.get(cur, 'width');
       }, 0);
     }),
     sizeArray: [
       Size.create({height: 10, width: 10}),
       Size.create({height: 10, width: 10})
     ]
   }).create();
   var sizeArray = rectangle.get('sizeArray');
 
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200");
 
   sizeArray.pushObject(Size.create({height: 10, width: 10}));
   assert.equal(rectangle.get('totalArea'), 300, "the total area should be 300 after added");
 
   sizeArray.removeAt(0);
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200 after removed");
 
   sizeArray[0].set('height', 20);
   assert.equal(rectangle.get('totalArea'), 200, "the total area should not be changed");
 
   sizeArray.clear();
   assert.equal(rectangle.get('totalArea'), 0, "the total area should be 0 after reset rectangle.sizeArray");
 });

[参考代码](https://github.com/Cuiyansong/ember-table-learnning/blob/master/ember-test/tests/unit/computed-property-test.js#L104)

 

3. Array.[]特性

当CP属性依赖于.property('columns.[]')时:

  - 与绑定.property(‘columns.@each') 行为相同。

 test('computed property depend on []', function (assert) {
   var Size = Ember.Object.extend({height: 0, width: 0});
   var rectangle = Ember.Object.extend({
     totalArea: Ember.computed('sizeArray.[]', function () {
       return this.get('sizeArray').reduce(function (prev, cur) {
         return prev + Ember.get(cur, 'height') * Ember.get(cur, 'width');
       }, 0);
     }),
     sizeArray: [
       Size.create({height: 10, width: 10}),
       Size.create({height: 10, width: 10})
     ]
   }).create();
   var sizeArray = rectangle.get('sizeArray');
 
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200");
 
   sizeArray.pushObject(Size.create({height: 10, width: 10}));
   assert.equal(rectangle.get('totalArea'), 300, "the total area should be 300 after added");
 
   sizeArray.removeAt(0);
   assert.equal(rectangle.get('totalArea'), 200, "the total area should be 200 after removed");
 
   sizeArray[0].set('height', 20);
   assert.equal(rectangle.get('totalArea'), 200, "the total area should not be changed");
 
   sizeArray.clear();
   assert.equal(rectangle.get('totalArea'), 0, "the total area should be 0 after reset rectangle.sizeArray");
 });

[参考代码](https://github.com/Cuiyansong/ember-table-learnning/blob/master/ember-test/tests/unit/computed-property-test.js#L134)

 

(Ember v2.0.0)

 

2015-08-17 除建议用[]代替@each,暂未发现其他变化。

[参考代码](https://github.com/emberjs/ember.js/blob/v2.0.0/packages/ember-metal/lib/computed.js#L224)

注: 更多关于计算属性问题,请参考文章《Ember.js之computed Property-3.CP重要原则》

 

三、源码分析Array.[] & Array.@each

Array.@each返回的是一个特殊类型,这个类型便于实现绑定。

   '@each': computed(function() {
     if (!this.__each) {
       // ES6TODO: GRRRRR
       var EachProxy = requireModule('ember-runtime/system/each_proxy')['EachProxy'];
 
       this.__each = new EachProxy(this);
     }
 
     return this.__each;
   })

[参考这里](https://github.com/emberjs/ember.js/blob/stable-1-13/packages/ember-runtime/lib/mixins/array.js#L516)

 

Array.[]继承自Ember.Enumerable.[]并且返回this。

   '[]': computed({
     get(key) {
       return this;
     },
     set(key, value) {
       this.replace(0, get(this, 'length'), value);
       return this;
     }
   }),

[参考这里](https://github.com/emberjs/ember.js/blob/stable-1-13/packages/ember-runtime/lib/mixins/array.js#L164)

 

二者之间唯一的区别是Array.[]返回的是一对象自身(普通对象组成的数组), 而Array.@each返回的是EachProxy对象, 针对普通对象组成的数组而言,仅仅能检测到数组长度的变化和对象本身的变化,对数组内容发生变化则不得而知,而EachProxy对每一个元素增加observerListener监听器,当数组内容发生变化时,通知数组发生变更,实现了数组元素这一级别的监听。

 var EachProxy = EmberObject.extend({
 
   init(content) {
     this._super(...arguments);
     this._content = content;
     content.addArrayObserver(this);
 
     // in case someone is already observing some keys make sure they are
     // added
     forEach(watchedEvents(this), function(eventName) {
       this.didAddListener(eventName);
     }, this);
   },
 
 ...

[参考这里](https://github.com/emberjs/ember.js/blob/stable-1-13/packages/ember-runtime/lib/system/each_proxy.js#L109)

 

四、相关引用

[Ember-@each](http://emberjs.com/api/classes/Ember.Array.html#property__each)

[Emer-EachProxy](https://github.com/emberjs/ember.js/blob/stable-1-13/packages/ember-runtime/lib/system/each_proxy.js)

[Ember-study](https://github.com/Cuiyansong/ember-table-learnning)

 

关键词:数组