简体   繁体   English

Ember.js:无路径过渡路线

[英]Ember.js: Pathless Transitional Route

New to ember.js. ember.js的新功能。 What I'm trying to do is: create a transitional route that has no path, that I can pass an AJAX promise to as the model when I transition to it, and then it makes a redirect decision once the promise completes. 我正在尝试做的是:创建一个没有路径的过渡路线,当我过渡到该模型时可以将AJAX Promise作为模型传递给它,然后在Promise完成后做出重定向决定。 I want the LoadingRoute view to be invoked while this is happening. 我希望在发生这种情况时调用LoadingRoute视图。 I've tried to accomplish that with the following route: 我尝试通过以下路线实现这一目标:

App.LoginUserRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        //Model should be a promise
        model.then(this.success.bind(this),this.failure.bind(this));
    },

    success: function (data) {
        if (data.success) {
            App.loggedInUser = App.User.create(data);
            console.log(App.loggedInUser);
            //Make redirection decision
        }
        else {
            alert(data.error);
        }
    },

    failure: function () {
        //Failure code
    }
});

However, when I try to pass the promise to the route like follows: 但是,当我尝试将诺言传递给路线时,如下所示:

var request = $.post("api.php", {action: 'create_user', username: this.username, password: this.password});
this.transitionToRoute('loginUser',request);

I get the error "More context objects were passed than there are dynamic segments for the route: loginUser" because I'm trying to create a pathless route and Ember requires that the model be serialized into the path when passed using transitionToRoute(). 我收到错误消息“传递的上下文对象多于该路由的动态段:loginUser”,因为我试图创建无路径路由,Ember要求在使用transitionToRoute()传递时将模型序列化到路径中。

The reason I need this is: 我需要这个的原因是:

  • The login event can happen from multiple controllers (when the user registers, when they login using the login screen, or when the application first loads if the right cookie is detected) and I don't want to duplicate the login logic across all those controllers. 登录事件可能来自多个控制器(当用户注册,当用户使用登录屏幕登录时,或者如果检测到正确的cookie时首次加载应用程序时),我不想在所有这些控制器上重复登录逻辑。
  • After the login completes, there's multiple different routes the user could then be directed to, depending on the nature of the returned user object. 登录完成后,根据返回的用户对象的性质,可以将用户定向到多种不同的路由。
  • I want the LoadingRoute to be invoked while the request is completing. 我希望在请求完成时调用LoadingRoute。

I assume the solution to my problem is not to use routing, but rather something else. 我认为解决我的问题的方法不是使用路由,而是使用其他方法。 However, I'm not sure what the "something else" would be. 但是,我不确定“其他”会是什么。

You'll want to take advantage of a mixin, and hooking into the transition route. 您将需要利用mixin,并加入过渡路线。

The following SO answer will work for all of your needs: 以下SO答案将满足您的所有需求:

Ember Client Side Authentication, route authentication Ember客户端身份验证,路由身份验证

In the end I achieved what I was trying to do by adding the following code to ApplicationController 最后,通过将以下代码添加到ApplicationController中,我实现了我想做的事情

loginUser: function (request) {
    this.transitionToRoute('loading');
    request.then(this.loginRequestSuccess.bind(this),this.loginRequestFailure.bind(this));
},

loginRequestSuccess: function (data) {
    if (data.success) {
        App.loggedInUser = App.User.create(data.user);
        console.log(App.loggedInUser);
        //Route transition decision
    }
    else {
        alert(data.error);
    }
},

loginRequestFailure: function () {
    //Failure code
}

And then calling the loginUser() function with the jqXHR object from wherever in the code a login request was made. 然后从代码中的任何位置调用jqXHR对象调用loginUser()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM