ember.js中'[]'属性和'@each'属性有什么区别? [英] What is the difference between the '[]' property and the '@each' property in ember.js?

查看:109
本文介绍了ember.js中'[]'属性和'@each'属性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,可枚举的mixin已经计算出取决于'[]'属性的属性,而ember数组也有 @each'属性



根据'[]'有什么区别?'@ each'



我的模糊的理解(如果我错了,纠正我)是'[]'阵列内容被替换。但这是不同的,取决于物业本身?



考虑以下类:

  C = Ember.Object .extend({
something:null,
watcher1:(function(){
console.log('watcher1')
})。observes('things。[] )
watcher2:(function(){
console.log('watcher2')
})。观察('something。@ each')
});

我创建一个如下实例:

  c = C.create({things:Ember.A(['a','b'])}); 

以下内容:

  c.get('things')。replace(0,1,['z'])

触发器 watcher1 watcher2



和以下内容:

  c.get('things')。setObjects(['1','2']) 

还触发 watcher1 watcher2



如下:

  c.get('something')。addObject('v')

那么有没有区别?我们什么时候应该使用一个对另一个?



谢谢!
Kevin

解决方案

使用 @each if您需要观察数组元素的属性



@each 支持观察内部元素的属性阵列。例如, people。@ each.name 。括号符号不支持这一点。这是一个 jsbin演示



@each 是一个 Array实例的属性,返回一个 EachProxy 实例。另一方面, [] 只是返回这个



使用 [] 如果您需要它在非数组枚举上工作



根据ember更改日志,括号符号被废止,以支持 @each 在ember 0.9.4 ,但在 0.9.8 重新启用它的提交表示 [] 可以用于非数组枚举,例如 Ember.Set 实例。 jsbin演示


I've noticed that the enumerable mixin has computed properties that depends on the '[]' property, while ember arrays also have the '@each' property.

What is the difference between depending on '[]' and '@each'?

My vague understanding (correct me if I'm wrong) is that '[]' is triggered when the array content is replaced. But is this different than depending on the property itself?

Consider the the following class:

C = Ember.Object.extend({
  things: null,
  watcher1: (function() {
    console.log('watcher1')
  }).observes('things.[]'),
  watcher2: (function() {
    console.log('watcher2')
  }).observes('things.@each')
});

And I create an instance as follows:

c = C.create({things: Ember.A(['a', 'b'])});

The following:

c.get('things').replace(0, 1, ['z'])

triggers watcher1 and watcher2

And the following:

c.get('things').setObjects(['1', '2'])

also triggers watcher1 and watcher2

As does:

c.get('things').addObject('v')

So is there any difference? When should we use one vs. the other?

Thanks! Kevin

解决方案

Use @each if you need to observe properties of array elements

@each supports observing properties of the elements inside the array. For example, people.@each.name. The bracket notation does not support this. Here is a jsbin demo.

@each is a property of Array instances that returns an EachProxy instance that handles the delegation. On the other hand, [] simply returns this.

Use [] if you need it to work on non-Array enumerables

According to the ember changelog, the bracket notation was made defunct in favor of @each in ember 0.9.4, but then re-enabled in 0.9.8. The commit that re-enables it indicates that [] can be used for non-Array enumerables such as Ember.Set instances. jsbin demo.

这篇关于ember.js中'[]'属性和'@each'属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆