2013-02-11 43 views
7

我对ember.js相当陌生,我正在做一些实践。我最近在试图删除记录时遇到了一些困难。这里是我的编辑路线(从我称之为删除)删除记录时,转换到另一条路由失败

App.PostsEditRoute = Ember.Route.extend({ 
    model: function(params){ 
    return App.Post.find(params.id); 
    }, 
    exit: function() { 
    this._super(); 
    tx = this.get('currentModel.transaction'); 
    if(tx) 
     tx.rollback(); 
    }, 
    setupController: function(controller, model){ 
    controller.set('content', model); 
    }, 
    events: { 
    save: function(post){ 
     post.one('didUpdate', this, function(){ 
     this.transitionTo('posts.show', post); 
     }); 
     post.get('transaction').commit(); 
    }, 
    cancel: function(){ 
     this.transitionTo('posts.show', post); 
    }, 
    destroyPost: function(context) { 
     var post = context.get('content'); 
     post.deleteRecord(); 
     post.get('store').commit(); 
     this.transitionTo('posts.index'); 
    } 
    } 
}); 

所以我要通过我触发destroyPost的链接。记录被成功删除,并且我开始转换到索引路由,但发生错误...

未捕获错误:试图在处于状态rootState.deleted.inFlight时处理事件rollback。用undefined调用

在此之后,加载索引页的模型停止,我得到一个空白页。我可以提供任何所需的附加代码。这是指数路线。

App.PostsIndexRoute = Em.Route.extend({ 
    model: function(){ 
    return App.Post.find(); 
    }, 
    setupController: function(controller, model){ 
    controller.set('content', model); 
    } 
}); 

我应该注意到这两条路由都是正确加载的。只有在转型中我才会失败。

回答

4

您需要绑定到didDelete,就像您在保存方法中使用didUpdate一样。

destroyPost: function(context) { 
    var post = context.get('content'); 
    post.one('didDelete', this, function() { 
     this.transitionTo('posts.index'); 
    }); 
    post.deleteRecord(); 
    post.get('transaction').commit(); 
} 

而且,你的代码似乎有点矛盾:在保存方法您提交的单独的事务,而在destroyPost你所提交的整个商店。

+1

这工作。我从其他地方拉代码;我只是试图在这一点上学习。不过,应该已经通过文档更好地阅读。感谢您指点我正确的方向。 – sentinel21 2013-02-16 00:31:49