储存余烬数据模型后的转换 [英] transition after saving model of ember data

查看:72
本文介绍了储存余烬数据模型后的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在创建帖子后进行转换。

I want to make transition after a create a post.


post / new>点击提交> rails后端成功创建帖子和响应在ember_data_example github源代码中将json>重定向到新创建的帖子的路径

post/new > click submit > rails backend successfully create post and response a json > redirect to newly created post's path

。他们使用这种方法

 transitionAfterSave: function() {
     // when creating new records, it's necessary to wait for the record to be assigned
     // an id before we can transition to its route (which depends on its id)
     if (this.get('content.id')) {
       this.transitionToRoute('contact', this.get('content'));
     }
 }.observes('content.id'),

它工作正常,因为模型创建时模型的ID为null,当模型保存成功时,其ID会发生变化,因为该功能观察到模型ID的更改。

It works fine, because The model has ID of null when model created, and its ID would change when model saving is successful because this function observes change of models ID.

但是也许,只要模型的ID属性被更改,这个功能将被执行。
我找到一些更语义的方式。

But maybe, this function will be executed whenever model's ID property is changed. I'm finding some more semantic way.

当模型的状态更改为'isDirty'= false&&&&& 'isNew'== true form'isDirty'= true,'isNew'= false。

I want transition to be executed when the model's status is changed to 'isDirty' = false && 'isNew' == true form 'isDirty' = true, 'isNew' = false.

如何实现?

推荐答案

理想情况下,id不应该改变。但是,您是正确的,在语法上,这种方法似乎不正确。

Ideally, the id is not supposed to change. However, you are correct, semantically, this method doesn't seem right.

有一个更干净的方法来做到这一点:

There is a cleaner way to do this:

save: function(contact) {
  contact.one('didCreate', this, function(){
    this.transitionToRoute('contact', contact);
  });

  this.get('store').commit();
}

更新2013-11-27(ED 1.0测试版):

save: function(contact) {
  var self = this;
  contact.save().then(function() {
    self.transitionToRoute('contact', contact);
  });
}

这篇关于储存余烬数据模型后的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆