rksh
rksh

Reputation: 4050

Ember Transion-to URL not working

I have an ember action that submits a form using jquery post like this,

 submit : function(){   //submitting new story
        this.set('onsubmit' , true);
        var self = this;
        $.post( "/api/post", { some post data })
            .done(function(data) {
               self.transitionTo('post' + data);
            });
        }

The urls are like this, domain example.com. Post is located at example.com/api/post. After posting the user must be redirected to example.com/post/3432232 (some value returned by the post data).

However after posting this transition to "example.com/api/post/5000203" not example.com/post/234234234.

Which doesnt exists and gives a 404. How can I make the user go back to example.com/post/234234234 using transition-to function in ember?

Edit - Routes are as follows

App.Router.map(function() {    
this.resource('index', {path : '/'} , function() { 
    this.resource('p', function(){
        this.resource('post', { path: ':post_id' });
    });
});
});

Thanks

Upvotes: 0

Views: 69

Answers (1)

Microfed
Microfed

Reputation: 2890

In Ember routing system transitionTo takes as arguments route's name and route's model (the same model, that route returns when Ember.Route.model hook is invoked). There are two type of router's transitions in Ember's glossary: URL transition (happens when client handles new URL, fo ex., when you open a page from scratch) and "named transition" (which happens when this.tranisionTo is called).

Ember.Route tries to load model based on params list (:post_id, for example) while handling "URL transition". In other hand, while it handling "named transition" it doesn't call model hook, but uses already provided model (this.tranisition('post', PostModel)`). Docs.

So, if you have post_id on hands, just call this.store.find('post', postId) to get PostModel and provide requested model to PostRoute: this.transitionTo('post', this.store.find('post', data).

P.S. Please, consider reading this question and an answer to get your routing structure actually nested: Nested URLs: issues with route transition.

P.P.S. I looked at the Ember's documentation and found new (at least for me) signatures for transitionTo method: http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo. According to this doc, you can rewrite your example to get it work: this.tranisitionTo('post', data), where data is post_id.

Upvotes: 1

Related Questions