在emberJS中运行时生成方法

时间:2014-12-02 06:49:16

标签: javascript ember.js ember-data metaprogramming emblem.js

我试图在运行时在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")

但它没有生成方法。那么我有什么遗漏吗?

1 个答案:

答案 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')