【问题标题】:Exception with Computed Property on Ember Data Model using RESTAdapter使用 RESTAdapter 的 Ember 数据模型上的计算属性异常
【发布时间】:2013-05-23 04:04:56
【问题描述】:

所以我在使用 ember 数据和 RESTAdapter 的 ember.js 应用程序中基本上有以下两个模型。

App.JobsController = Ember.ArrayController.extend({
  completedCount: function() {
    return 0; //Doesn't matter what I return here.
  }.property('content.@each.state')
});

App.Job = DS.Model.extend({
  transactions: DS.hasMany('App.Transaction'),
  state: function() {
    return 0; //Doesn't matter what I do here
  }.property('transactions.@each.transactionType')
});

App.Transaction = DS.Model.extend({
  job: DS.belongsTo('App.Job'),
  transactionType: DS.attr('number')
});

我遇到的问题是transactions.@each.transactionType 的存在导致以下异常:

"Attempted to handle event `becomeDirty` on <App.Transaction:ember462:1> while in state rootState.loaded.materializing.firstTime. Called with undefined"

我在问题队列或堆栈溢出的其他任何地方都找不到对此的任何引用。我做错了什么?


更新

因此,我进行了更多调查,但在我的原始帖子中并没有把事情说得足够清楚。我已经更新了代码以反映它。

1) 我在计算属性中做什么并不重要。仅仅拥有它们就会导致异常。

2) 仅当存在另一个依赖于原始计算属性的计算属性时才会发生异常。 (希望这是有道理的)

所以在上面的例子中,JobsController 有一个基于 Job 模型的计算属性的计算属性。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    也可以根据 isLoaded 试试:

    state: function(){
      return this.get('isLoaded') ? this.get('transactions.lastObject.transactionType') : null;
    }.property('isLoaded', 'transactions.@each.transactionType')
    

    【讨论】:

    • 取决于 isLoaded 导致相同的attempted to handle event becameDirtyexceptoin
    【解决方案2】:

    因为 transactions 是一个 Ember 数组,你应该测试它的长度以确定他是否有可用的数据......尝试类似:

    state: function(){
        var transactions = this.get('transactions');
        var lastObject = transactions.get('length') > 0 ? transactions.get('lastObject') : null;
        return lastObject !== null ? lastObject.get('transactionType') : null;
    }.property('transactions.@each.transactionType')
    

    【讨论】:

    • 查看我更新的问题。我在属性中所做的并不是导致错误的原因,只是具有相关的计算属性会导致问题
    • 你试过用模型代替内容吗? .property('model.@each.state')
    猜你喜欢
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2014-04-01
    • 2014-10-09
    • 2017-04-11
    • 2014-12-21
    相关资源
    最近更新 更多