我试图在运行时在 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")
但它不生成方法。那么我有什么遗漏吗?
最佳答案
您正在将 that
定义为 Controller 上的属性,但试图将其用作 defineAttributes
方法中的局部变量。将 that
更改为方法中的局部变量,它应该可以正常工作。或者更好的是,只需使用 Coffeescript 的粗箭头函数来维护 this
的当前值:
defineAttributes: (->
['unsubmitted', 'submitted', 'cancelled'].forEach (f) =>
Em.defineProperty this, f, Em.computed.filterBy('model', f, true)
).on('init')
关于javascript - 在 emberJS 中运行时生成方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27243460/