Ember computed/get 方法有更短的写法吗?
Is there a shorter way to write Ember computed/get method?
是否有一种简短的方法来编写下面的代码(来自 Rock & Roll with Ember.js 书):
export default Ember.Controller.extend({
noSongs: Ember.computed('model.songs.length', function() {
return this.get('model.songs.length') === 0;
}),
(...)
});
我想我可以解构导入 Controller
、get
和 computed
:
import { Controller, get, computed } from 'ember';
问题
- 我可以在
computed
中使用箭头函数而不是 function
吗?
- 是否有更 javascript 的方法来做
this.get('model.songs.length')
因为将 length
作为字符串感觉很尴尬?
Can I use arrow function inside the computed instead of function?
不,你不能。
Is there a more javascript-ish way to do
this.get('model.songs.length') as it feels awkward to have length as a
string?
恕我直言,这看起来不错。
Can I use arrow function inside the computed instead of function?
您作为 computed()
方法的第二个参数传递的函数在 Ember 的内部用作 Ember 创建中的 属性 属性 描述符对象。当稍后调用它时(从内部开始)Ember 尝试为其分配适当的上下文(在本例中是您的控制器)但是箭头函数不支持使用上下文调用,因此它们只会 运行 在调用它们的上下文中。
Is there a more javascript-ish way to do this.get('model.songs.length') as it feels awkward to have length as a string?
那是 Ember。在你使用它一千一千万次之后,它会感觉很自然。
I reckon I can desctructure import for Controller
, get
and computed
:
您可以使用新模块 API,详见 2.16 release blog post。
Can I use arrow function inside the computed
instead of function
?
不,因为箭头函数的上下文实际上是周围的模块,而不是对象。请注意,Ember.Controller.extend
接收一个对象作为参数,而不是函数,并且它不是 ES2015 引入的 class
语法。
Is there a more javascript-ish way to do this.get('model.songs.length')
as it feels awkward to have length as a string?
Length 不是字符串,它是 model.songs
数组的 属性,应该有一个数值。您应该在 Ember 对象中使用 .get
,否则您将无法检索计算属性。
是否有一种简短的方法来编写下面的代码(来自 Rock & Roll with Ember.js 书):
export default Ember.Controller.extend({
noSongs: Ember.computed('model.songs.length', function() {
return this.get('model.songs.length') === 0;
}),
(...)
});
我想我可以解构导入 Controller
、get
和 computed
:
import { Controller, get, computed } from 'ember';
问题
- 我可以在
computed
中使用箭头函数而不是function
吗? - 是否有更 javascript 的方法来做
this.get('model.songs.length')
因为将length
作为字符串感觉很尴尬?
Can I use arrow function inside the computed instead of function?
不,你不能。
Is there a more javascript-ish way to do this.get('model.songs.length') as it feels awkward to have length as a string?
恕我直言,这看起来不错。
Can I use arrow function inside the computed instead of function?
您作为 computed()
方法的第二个参数传递的函数在 Ember 的内部用作 Ember 创建中的 属性 属性 描述符对象。当稍后调用它时(从内部开始)Ember 尝试为其分配适当的上下文(在本例中是您的控制器)但是箭头函数不支持使用上下文调用,因此它们只会 运行 在调用它们的上下文中。
Is there a more javascript-ish way to do this.get('model.songs.length') as it feels awkward to have length as a string?
那是 Ember。在你使用它一千一千万次之后,它会感觉很自然。
I reckon I can desctructure import for
Controller
,get
andcomputed
:
您可以使用新模块 API,详见 2.16 release blog post。
Can I use arrow function inside the
computed
instead offunction
?
不,因为箭头函数的上下文实际上是周围的模块,而不是对象。请注意,Ember.Controller.extend
接收一个对象作为参数,而不是函数,并且它不是 ES2015 引入的 class
语法。
Is there a more javascript-ish way to do
this.get('model.songs.length')
as it feels awkward to have length as a string?
Length 不是字符串,它是 model.songs
数组的 属性,应该有一个数值。您应该在 Ember 对象中使用 .get
,否则您将无法检索计算属性。