计算属性中的 `属性.[]` 和 `属性` 有什么区别
What are the difference between `property.[]` and `property` in computed properties
使用 ember.js,使用计算属性,我可以使用以下任一方法:
myProp: computed('properties', function () { /* do stuff. */ });
或者这样:
myProp: computed('properties.[]', function () { /* do stuff. */ });
这两种方法有什么区别?在什么情况下应该使用哪一个?
第一个 (properties
) 刚看了 properties
。所以当你做 this.set('properties', ...)
.
时它会失效
第二个用于数组,当您删除或添加数组项时将失效:
this.set('properties', ...);
this.properties.pushObject(...);
this.properties.popObject(...);
所以当你只是做 'properties'
它不会在你从数组中添加或删除一些东西时失效。
使用 ember.js,使用计算属性,我可以使用以下任一方法:
myProp: computed('properties', function () { /* do stuff. */ });
或者这样:
myProp: computed('properties.[]', function () { /* do stuff. */ });
这两种方法有什么区别?在什么情况下应该使用哪一个?
第一个 (properties
) 刚看了 properties
。所以当你做 this.set('properties', ...)
.
第二个用于数组,当您删除或添加数组项时将失效:
this.set('properties', ...);
this.properties.pushObject(...);
this.properties.popObject(...);
所以当你只是做 'properties'
它不会在你从数组中添加或删除一些东西时失效。