1

我在使用计算属性时遇到问题。

这是对ArrayController. 问题是,Ember 会在数据加载之前尝试计算它。例如,其中一部分是

var counts = this.getEach('hours').forEach(function(hours) {
  var d = hours.find(function(_hour) {
    return +(_hour.date.substring(11, 13)) === 10;
  });
  return d.count;
});

我收到一个错误,因为this.getEach('hours')返回类似

[ Array[24], undefined ]

在加载 AJAX 请求时,代码会中断。

我敢肯定其他人以前遇到过这种情况-解决方案是什么?

更新:这是我获取数据的方式。当用户在视图中单击一个月时,我将单击的月份的 id 传递给我的MonthsController. 它有一个toggleMonth方法:

App.MonthsController = Ember.ArrayController.extend({
    toggleMonth: function(id) {
        var month = App.Month.find(id),
            index = this.indexOf(month);
        if (index === -1) {
            this.pushObject(month);
        } else {
            this.removeAt(index);
        }
    }
});

App.Month.find(id)发送正确的 AjAX 请求 + 数据返回,但这可能不是填充月份控制器的正确方法。

此外,这发生在IndexRoute(即我没有单独的路由.MonthsController所以,我从不指定模型钩子或setupController.MonthsController

4

1 回答 1

0

解决这个问题的一般方法是promise:异步请求立即返回一个promise,它基本上是一个值的promise,可以在以后解决。所有 Ember 模型都是幕后的承诺。将ember模型视为 promises,以及Ember 的 Promises 与一般的 Promises 有何关系,特别是 jQuery 的 Promises?

你能解释一下第一段代码的上下文吗?this该块是什么this.getEach('hours').forEach以及何时执行?

于 2013-07-27T20:21:27.073 回答