在Ember.js中设置location.href

时间:2014-12-01 10:37:57

标签: javascript ember.js

我正在开发一个Ember.js应用程序,其初始页面位于:

https://localhost:8443/

如果用户经过身份验证,我想执行从JavaScript重定向到以下位置:

https://localhost:8443/admin

为此,我按如下方式编写代码:

App.ApplicationRoute = Ember.Route.extend({
  model: function () {
    var that = this,
        session = this.get('session'),
        sessionService = this.get('services').session();

    return sessionService.getStatus().then(function (sessionStatus) {
      session.setProperties(sessionStatus);
      if (/^\/[signup]|[forgotPassword]/.test(that.router.get('url'))) return;
      if (!session.get('authenticated'))
        that.transitionTo('signin');
      else
        window.location.href = window.location.origin + '/admin';
    });
  }
});

然而,当我执行此操作时,页面浏览到:

https://localhost/admin

我也试过location.assign()同样的结果。

为什么设置location.hreflocation.assign()会从网址中删除端口号?这是Ember.js路由器会做的事情吗?

我可以通过其他方式实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

您必须查看documentation for the Location object才能了解原因。从本质上讲,有方法可以获取 URL的每个部分,而您恰好使用了错误的部分。尝试其中之一:

window.location.href = window.location.host + '/admin';

或者,更优选:

window.location.href = '/admin';

第二种方式很好,因为它允许你完全忘记基本URL,只是担心你需要的相对路径。

此外,Ember.js根本不会影响这种功能,你只是处理Javascript浏览器API。