.property()有什么作用?在function(){}。property()中 [英] What does .property() do? in function(){}.property()
问题描述
Todos.TodoController = Ember.ObjectController.extend({
isCompleted: function(key, value){
var model = this.get('model');
if (value === undefined) {
// property being used as a getter
return model.get('isCompleted');
} else {
// property being used as a setter
model.set('isCompleted', value);
model.save();
return value;
}
}.property('model.isCompleted')
});
我正在使用Ember.js的ToDo指南,但似乎无法理解该控制器有效。 .property()是什么意思?而当我删除返回值行时,功能如何保持不变。如果有人可以确切解释这里发生的事情,那将是很好的。
I'm working through the ToDo guide for Ember.js and I can't seem to understand how this controller works. What does the .property() mean? And how come when I remove the 'return value;' line the functionality stays the same. If someone could explain exactly what's going on here that would be great.
链接至指南:http://emberjs.com/guides/getting-started/marking-a-model-as-complete-incomplete/
推荐答案
在javascript中,获取或设置一个属性时进行某些处理的唯一方法是使用 Object.defineProperty :
In javascript the only way to do some processing when getting or setting one property is using Object.defineProperty:
Object.defineProperty(person, "b", {
get : function() {
return person.firstName + ' ' + person.surname;
},
set : function(newValue) {
var names = newValue.split(' ');
person.firsname = names[0];
person.surname = names[1];
},
enumerable : true,
configurable : true
});
但这有一些缺点:
- 不是跨浏览器
- 没有绑定,也就是说,如果
firstname
或姓氏
发生更改,相关属性全名
保持不变。 - 调用
person.name
当未定义person
时,抛出错误 - 不可能触发观察者,没有其他代码,也没有意识到依赖等级:
名字
取决于全名
,
- Isn't cross browser
- Doesn't have binding, in other words, if
firstname
orsurname
changes, the dependent propertyfullname
isn't changed. - Calling
person.name
whenperson
is undefined, make an error to be throwed - Isn't possible to trigger observers, no without additional code and aware of the depency hierachy:
firstname
depends fromfullname
, and it can be dependency of others properties arghhh!
由于这个Ember具有属性的概念,称为计算的属性。
Due to this Ember has the concept of "property", called computed property.
它可以用两种方式声明:
It can be declared in 2 ways:
foo: Ember.computed(function({
...
}).property(dependent keys);
或使用(默认) Ember.ENV.EXTEND_PROTOTYPES = true
:
foo: function() {
...
}.property(dependent keys);
属性(依赖键)
是之所以需要它,是因为它告诉我们改变属性时会改变的属性。
The property(dependent keys)
, is needed because it tell to ember what is the properies that when changed, will make the property be updated.
fullname: function(key, value) {
// setter
if (value !== undefined) {
var names = value.split(' ');
this.set('firstname', names[0]);
this.set('surname', names[1]);
}
// always return the complete result, so nexts calls to this.get('fullname') will return the cached value
return this.get('firstname') + ' ' + this.get('surname');
}.property('firstname', 'surname')
使用此功能,您可以
- 的优势,当更改
名
或姓时
更改为新值,全名
。 -
beforeObserves
在更改值之前被触发,而observs
被触发 - 引用某些属性的任何模板都会更新
- 然后再调用一次person.get('first''),将会返回一个缓存的值,保存处理。您可以使用
.property(..)。volatile()
- 禁用此属性,当访问诸如null之类的对象时,请避免null或未定义的错误:
controller.get('person.dog.name')
如果未定义人员或狗,则返回未定义
。
- when changing
firstname
orsurname
to a new value,fullname
is changed. - The
beforeObserves
are triggered before changing the value, and theobserves
are triggered after the value change. - Any template referencing some property is updated
- More then one call to person.get('firstname'), will return a cached value, saving processing. You can disable this using
.property(..).volatile()
- Avoid null or undefined errors, when accessing null objects like:
controller.get('person.dog.name')
returnsundefined
, if person or dog is undefined.
我希望对您有所帮助
这篇关于.property()有什么作用?在function(){}。property()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!