Ember.js版本升级后的数据转换 [英] Ember.js data transition after version upgrade

查看:67
本文介绍了Ember.js版本升级后的数据转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在写一个POC应用程序,在将Ember库升级到RC1之后,我遇到了一个问题。我注意到,当我转换到新版本的路由时,该对象的字符串版本似乎显示在URL中,如此...

So I'm writing a POC app, and I am running into an issue after upgrading my Ember library to RC1. I noticed that when I was transitioning to a route in the new version, a stringified version of the object appears to show up in the URL, like so...

http://localhost:3000/posts/<App.Post:ember269:511401b8c589137c34000001>

当转换到这样的路由时,路由工作成功,但显然尝试访问一个这样的URL时间不行。所以我想我会编辑我的代码转换到ID。

The routes work successfully when transitioned to like this, but obviously trying to visit a URL like that a second time won't work. So I figured I would edit my code to transition to the ID instead.

对于我的编辑路线,我有以下保存事件。

For my edit route, I have the following save event.

  events: {
    save: function(post){
      post.one('didUpdate', this, function(){
        this.transitionTo('posts.show', post);
      });
      post.get('transaction').commit();
    }
  }

当转换发生时,会产生如上所述的URL 。所以我更正了以下...

This produces a URL like the above when the transition happens. So I corrected it to the following...

  events: {
    save: function(post){
      post.one('didUpdate', this, function(){
        this.transitionTo('posts.show', post.id);
      });
      post.get('transaction').commit();
    }
  }

这样会产生正确的URL格式,不产生任何输出。 (请注意,当我以正确的格式访问第一次的URL时,显示输出DOES会产生输出,只是当我从编辑路由转换时)。 / p>

This produces the correct URL format, but the show route doesn't produce any output. (note that show output DOES produce output when I visit the URL for the first time with a correct format, just not when I transition to it from the edit route).

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

所以我很困惑。任何洞察这个问题的原因(如果你知道为什么RC产生它)将非常感激。帮我吃蛋糕也吃谢谢!

So I'm confused. Any insight into the cause of this problem (and if you know why the RC produces it) would be much appreciated. Help me have my cake and eat it, too. Thanks!

推荐答案

从您的 App.PostsShowRoute 我可以猜到你设置您的路由映射如下:

From your App.PostsShowRoute I can guess that you set up your route mapping like this:

App.Router.map(function() {
  this.resource('posts', function() {
   this.route('show', { path:'/:id' });
  });
});

您需要更改:id to :post_id

App.Router.map(function() {
 this.resource('posts', function() {
   this.route('show', { path:'/:post_id' });
 });

});

现在,由于您使用Ember约定,您可以通过删除整个 App.PostsShowRoute = Em.Route.extend ... ,因为Ember可以为您处理。

Now, since you are using Ember conventions, you can take advantage of that by deleting the whole App.PostsShowRoute = Em.Route.extend ... because Ember can take care of it for you.

并使用您的第一种方法是正确的:

And use your first method which was correct:

events: {
  save: function(post){
    post.one('didUpdate', this, function(){
      this.transitionTo('posts.show', post);
    });
    post.get('transaction').commit();
  }
}

这篇关于Ember.js版本升级后的数据转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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