Ember: route ID is lost after a transition.retry() - Am I doing something wrong?

微笑、不失礼 提交于 2019-12-13 17:57:59

问题


See JSFiddle: http://jsfiddle.net/cyclomarc/MHvrH/3/

There are a lot of samples that show how to implement authentication in Ember. To protect access to a route, you will be redirected to a login page if you do not have a valid token. After succesful login (thus after obtaining a valid token), you will be redirected to the initial requested page.

Implementation: before the redirect to login, you store the requested transition in an object; after login, you read the object property and do a transition.retry.

login: function () {
    var self = this;

    App.Session.set('token', '1234567890');
    var attemptedTransition = App.Session.get('attemptedTransition');

    if (attemptedTransition) {
      attemptedTransition.retry();
    }
    ....

This works well if you access the application via the root URL. You can see this in the JSFiddle. Click on publications to see a list of publications. Then, if you click a publication to see the details you have to login. Click on login (just simulates a succesful login) and you are transitioned to the "details" route.

If you however access a "detail" URL directly (e.g. browse to http://yyy/index.html#/publications/1), then the .retry on the stored transition fails. It seems like in that case, the ID (param of the route) is lost. The url becomes: http://yyy/index.html#/publications/undefined ...

Is this a known problem ? Is there a workaround ?

FURTHER INFO:

If you inspect the transition then in case it does not works, the providedModelsArray is not set. Has this something to do with Ember.data ?

Correct transition (Ember.inspect(savedTransition)):

    {router: [object Object], promise: [object Object], 
data: [object Object], resolvedModels: [object Object], 
providedModels: [object Object], 
providedModelsArray: <App.Publication:ember330:2>, 
sequence: 2, params: [object Object], 
targetName: publications.show, isAborted: true} 

Incorrect transition (Ember.inspect(savedTransition)):

    {router: [object Object], promise: [object Object], 
data: [object Object], resolvedModels: [object Object], 
providedModels: [object Object], 
providedModelsArray: , 
sequence: 1, params: [object Object], 
targetName: publications.show, 
urlMethod: null, isAborted: true} 

回答1:


Reason of this problem is the find used in the model of the show route. You should not use findById(id). The correct use is: App.Publication.find(params.publication_id).



来源:https://stackoverflow.com/questions/18297560/ember-route-id-is-lost-after-a-transition-retry-am-i-doing-something-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!