使用Ember CLI的计算属性宏

时间:2014-07-25 16:32:06

标签: ember.js ember-cli

我试图干掉我的应用程序并使用Ember CLI将一些功能转移到宏中。在阅读this article之后,我认为我可以让事情有效,但在尝试将宏与任何参数一起使用时,我遇到undefined is not a function TypeError: undefined is not a function错误。如果我没有传递任何论据,那么ember就不会抛出错误。要使用命令ember generate util calc-array

生成文件,请执行此操作
// utils/calc-array.js
import Ember from 'ember';

export default function calcArray(collection, key, calculation) {
    return function() {
        ...
    }.property('collection.@each');
}

// controller/measurements.js
import Ember from 'ember';
import calculate from '../../utils/calc-array';

export default Ember.ArrayController.extend({
    high: calculate(this.get('model'), 'value', 'high'),
    ...
});

1 个答案:

答案 0 :(得分:2)

this.get('model')导致问题 - this指向全局对象,而不是控制器实例。传递字符串(即model)并在计算属性中使用this.get

collection.@each也不起作用,它不是有效路径。

总结:

 export default function calcArray(collectionPath, key, calculation) {
   return function() {
     var collection = this.get(collectionPath);
     ...
   }.property(collectionPath + '.@each');
 }
相关问题