Creating methods at runtime in emberJS
I am trying to create multiple methods at runtime in ember and the code I am trying to do
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")
But its not generative methods. So is there something I can't see?
+3
source to share
1 answer
You are defining that
as a property on your controller, but trying to use it as a local variable in your method defineAttributes
. Change that
to a local variable in the method and it should work fine. Or better yet, just use Coffeescript's fat arrow function to maintain the current value this
:
defineAttributes: (->
['unsubmitted', 'submitted', 'cancelled'].forEach (f) =>
Em.defineProperty this, f, Em.computed.filterBy('model', f, true)
).on('init')
+3
source to share