2

I have a save event set up with my post creation route.

App.PostsNewRoute = Ember.Route.extend({
  model: function(){
    return App.Post.createRecord();
  },
  exit: function() {
    this._super();
    this.get('currentModel.transaction').rollback();
  },
  setupController: function(controller, model){
    controller.set('content', model);
  },
  events: {
    save: function(post){
      post.one('didCreate', this, function(){

        this.transitionTo('posts.show', post);
      });
      post.get('transaction').commit();
    },
    cancel: function(){
      this.transitionTo('posts.index');
    }
  }
});

Here is my post model.

App.Post = DS.Model.extend({
  body: DS.attr('string'),
  headline: DS.attr('string')
});

As you can see, it transitions to the show route after the post is created. I'm running into an issue where since the id of the post is undefined (it's defined after creation of the object in the database) the transition changes the url to /null . This seems logical, but obviously not ideal.

What is the best way to transition to the id of the new post? I return the whole object after it's created (with the ID included). Seems like Ember doesn't see the new object I'm returning.

sentinel21
  • 546
  • 7
  • 24
  • Can you create a jsfiddle with your issue? – Shimon Rachlenko Feb 27 '13 at 13:46
  • It'll be difficult to demonstrate because it's partially related to calling the RESTful methods on my server. It's difficult because I'm creating a new record on my server. The issue is that I don't set an ID for that record until I commit the record to the server (in other words, Ember never hears about the id -- it's created right in the database, not on the client). So when I transition to the new record, the id for that record is null, so it shows as null in the URL. I'm not sure I'm handling this the best way. I'll update my post with more of the routing code I'm using. – sentinel21 Feb 27 '13 at 16:37

1 Answers1

2

I was running into this. https://github.com/emberjs/data/issues/405 Looks like the didCreate event is firing before the object is updated. The workaround I used was just setting up an observer for the id in question. Then didCreate would fire, and when the id was updated, my observer fired, and within that observer I did the transition to the show route.

sentinel21
  • 546
  • 7
  • 24