Koa*_*la7 0 javascript model-view-controller ember.js jsbin
当我在我的应用程序中删除记录时,我有一个问题,我知道发生这种情况的原因,但我无法解决问题
我在这里转载了这个案子,
我添加一个发票,然后我添加更多的交易.
我删除发票后问题就出现了,这是错误信息
未捕获错误:断言失败:错误:尝试didSetProperty在状态root.deleted.saved中处理事件.使用{name:transactionsAmounts,oldValue:NaN,originalValue:undefined,value:0}调用.
删除发票时,基本上存在错误模型{transactionsAmount}
transactionAmounts是任何单个事务的总和,并在模型中创建
transactionsAmounts: DS.attr('string'),
setTransactionAmount : function(){
if(this.get("transactions.length")>0){
this.get("transactions").then(function(transactions){
var sum=0;
transactions.forEach(function(transaction){
sum+=transaction.get("total");
});
this.set("transactionsAmounts",sum);
}.bind(this));
}
}.observes('transactions.length', 'transactions.@each.total'),
Run Code Online (Sandbox Code Playgroud)
在删除发票的那一刻,不会删除transactionAmount,我怎么能发生这种情况才能删除发票模式(hasMany事务)并且没有得到错误?
这应该在Ember Data beta 16中修复.
因为在灰烬数据测试引入错误的14款删除仍然存在的集合,所以你必须让你的使用不会被删除确认的对象.这段代码为我修好了:
发票模型:
App.Invoice = DS.Model.extend({
title : DS.attr('string'),
transactions : DS.hasMany('transaction', { async:true}),
transactionsAmounts: DS.attr('string'),
setTransactionAmount : function(){
if(!this.get('isDeleted') && this.get("transactions.length") > 0){
this.get("transactions").then(function(transactions){
var sum=0;
transactions.forEach(function(transaction){
if(!transaction.get('isDeleted'))
{
sum += transaction.get("total");
}
});
if(!this.get('isDeleted'))
{
this.set("transactionsAmounts",sum);
}
}.bind(this));
}
}.observes('transactions.length', 'transactions.@each.total'),
});
Run Code Online (Sandbox Code Playgroud)
删除控制器中的操作:
remove: function() {
var transactions = this.get('model.transactions'),
list = transactions.toArray();
list.forEach(function(transaction) {
if (!transaction.get('isDeleted'))
{
transaction.deleteRecord();
transactions.removeObject();
}
});
var model = this.get('model');
if(!model.get('isDeleted'))
{
this.get('model').deleteRecord();
}
// and then go to the fatturas route
this.transitionToRoute('fatturas');
// set deleteMode back to false
this.set('deleteMode', false);
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2341 次 |
| 最近记录: |