【问题标题】:Computed Property Macros with Ember CLI使用 Ember CLI 计算属性宏
【发布时间】:2014-07-25 16:32:06
【问题描述】:

我正在尝试使用 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'),
    ...
});

【问题讨论】:

    标签: ember.js ember-cli


    【解决方案1】:

    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');
     }
    

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      相关资源
      最近更新 更多