我试图在运行时在ember和代码中生成一些方法我尝试
App.TestController = Ember.ArrayController.extend App.AnotherMixin,
unsubmitted: Em.computed.filterBy("model", "unsubmitted", true)
submitted: Em.computed.filterBy("model", "submitted", true)
canceled: Em.computed.filterBy("model", "canceled", true)
# Rather than using above methods I'm trying to generate them with meta-programming.
that: @
defineAttributes: (->
[
"unsubmitted"
"submitted"
"cancelled"
].forEach ( f ) ->
Em.defineProperty that , f, Em.computed.filterBy("model", f, true)
return
return
).on("init")
但它没有生成方法。那么我有什么遗漏吗?
答案 0 :(得分:3)
您将that
定义为控制器上的属性,但尝试将其用作defineAttributes
方法中的局部变量。将that
更改为方法中的局部变量,它应该可以正常工作。或者甚至更好,只需使用Coffeescript的胖箭头功能来保持this
的当前值:
defineAttributes: (->
['unsubmitted', 'submitted', 'cancelled'].forEach (f) =>
Em.defineProperty this, f, Em.computed.filterBy('model', f, true)
).on('init')