所以我在使用 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"
我在问题队列或堆栈溢出的其他任何地方找不到对此的任何引用。我做错了什么?
<小时/>更新
所以我做了更多的调查,但在我原来的帖子中没有把事情说得足够清楚。我已经更新了代码以反射(reflect)它。
1)我在计算属性中做什么并不重要。只要拥有它们就会导致异常。
2) 仅当存在另一个依赖于原始属性的计算属性时,才会发生异常。 (希望这是有道理的)
因此,在上面的示例中,JobsController 有一个计算属性,该属性基于 Job 模型的计算属性。
最佳答案
也尝试依赖 isLoaded:
state: function(){
return this.get('isLoaded') ? this.get('transactions.lastObject.transactionType') : null;
}.property('isLoaded', 'transactions.@each.transactionType')
关于ember.js - 使用 RESTAdapter 在 Ember 数据模型上计算属性时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705488/