Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 页面加载前/加载时余烬中的条件重定向_Ember.js - Fatal编程技术网

Ember.js 页面加载前/加载时余烬中的条件重定向

Ember.js 页面加载前/加载时余烬中的条件重定向,ember.js,Ember.js,我试图编写一些逻辑,如果用户登录(因此定义了currentUser变量,更多详细信息请参见下面的编辑部分),他应该无法访问应用程序的根URL,即登录页面。我试图通过重定向到用户来实现这一点。在页面加载之前/加载后立即显示页面,但我似乎不知道如何做到这一点 阅读ember文档,您可以从路由或控制器执行重定向。我两次都试过了,但都没有成功。在路线上: App.ApplicationRoute = Ember.Route.extend redirect: -> currentUser

我试图编写一些逻辑,如果用户登录(因此定义了
currentUser
变量,更多详细信息请参见下面的编辑部分),他应该无法访问应用程序的根URL,即登录页面。我试图通过重定向到
用户来实现这一点。在页面加载之前/加载后立即显示
页面,但我似乎不知道如何做到这一点

阅读ember文档,您可以从路由或控制器执行重定向。我两次都试过了,但都没有成功。在路线上:

App.ApplicationRoute = Ember.Route.extend
  redirect: ->
    currentUser = @controllerFor('Application').get('currentUser') # this works
    currentPath = @controllerFor('Application').get('currentPath') # this returns undefined
    if currentUser && currentPath == '/'
      @transitionTo('users.show', currentUser)
这里的问题是,由于某种原因,我无法读取当前路径,即使我可以从
App.ApplicationController
中访问它

在控制器中,我尝试了:

App.ApplicationController = Ember.Controller.extend
  init: -> # never gets called
    if @get('currentUser') && @get('currentPath') == '/'
      @transitionToRoute('users.show', @get('currentUser'))
除了
init
在加载页面时从未被调用,而且我无法从文档中找出在页面加载时调用哪个钩子

如果代码还有其他问题,请随时指出。如何修复代码以实现重定向

编辑:建议路由实现在
@controllerFor('application')
中将
'application'
更改为小写。尝试此操作后,返回错误

Assertion failed: The controller named 'application' could not be found. Make sure that this route exists and has already been entered at least once. If you are accessing a controller not associated with a route, make sure the controller class is explicitly defined.
不确定发生了什么,比如在所有小写字母中为其他路由执行
@controller,但不是
'application'

当前用户变量是通过在ember初始值设定项中的注入获得的。我在后台使用Rails和Desive,我将
currentUser
导入ember的方式基本上就是这样。一般流程是Rails->meta标记->初始化器->注入

解决方案:受接受答案后讨论的启发,我最终在
ApplicationRoute
中这样做:

App.ApplicationRoute = Ember.Route.extend
  beforeModel: (transition) ->
    currentUser = @controllerFor('currentUser')
    if currentUser && transition.targetName == 'index'
        @transitionTo('users.show', currentUser)

我没有看到您试图从应用程序路由获取应用程序控制器。我不能百分之百肯定这会奏效。我相信在调用
setupController
之前,
redirect
钩子在路由过程中很早就被调用了。这意味着
init
方法不太可能在
重定向之前被调用

我有两个可能的解决方案:

  • 似乎
    currentUser
    变量是从某种全局信息创建的。如果是这种情况,请在管线中创建它,必要时进行转换。如果仍然希望将数据注入控制器,请使用
    setupController
    挂钩将其放入控制器中

  • 如果
    currentUser
    变量恰好是该特定路由的模型,请尝试在
    afterModel
    钩子中进行转换。这样您就可以直接访问数据

  • 另外,我会尽量避免在控制器中出现这样的转换。如果可能的话,尽量让他们留在路线上

    如果您能给我一些关于
    currentUser
    变量来源的更多信息,我可能会给您一个更具体的答案

    编辑:这段代码来自您发布的文章:

    controller = container.lookup('controller:currentUser').set('content', user)
    
    如果这与您正在使用的类似,请尝试将其注入路由而不是控制器

    controller = container.lookup('route:currentUser').set('currentUserModel', user)
    
    然后,您可以在您的路径中直接使用
    @get('currentUserModel')
    。而且,如果出于某种原因,这不起作用,只需将用户放在全局范围内,以确保没有其他错误。因此,与上面的行不同:

    App.TEMP_CURRENT_USER = user;
    

    我认为您的问题在于您将
    应用程序
    大写。我不知道用户创建的路由,但是
    应用程序
    路由肯定都是小写的。请参阅我对原始问题底部的编辑。它似乎不喜欢
    App.ApplicationRoute
    中的
    @controllerFor('application')
    。我在原始问题中添加了一个关于
    currentUser
    的部分。它来自初始值设定项中的注入。实际上,我对注入的内部工作原理了解不多(除了它或多或少实现了我在这里想要的功能,即允许每个控制器访问一个
    currentUser
    变量),因此,如果您能够根据这些新信息详细说明您的答案,那将是一件非常棒的事情。如果您有更好的实现
    currentUser
    的方法,也可以很好地解决重定向问题,请随意分享!我更新了我的帖子。要点是在
    Ember.Application.initializer
    中,用户模型位于
    user
    变量中。从那里,你应该可以把它放在任何地方,方便以后访问。谢谢你的更新。这条线确实是我正在使用的。我可以把它改成你建议的,但我认为它解决不了我的问题。我确实已经可以通过
    @controllerFor
    ApplicationRoute
    访问
    currentUser
    变量,但问题是我无法获取
    currentPath
    。也许接下来的问题是,在路线中获得
    currentPath
    的最佳方式是什么?不确定
    currentPath
    应该是什么。这应该是当前URL吗?我相信
    currentPath
    ApplicationController
    中定义的返回当前URL的属性。如果我在
    ApplicationRoute
    中,并且我只想在用户位于主页时重定向,那么我需要以某种方式进行检查,对吗?