【问题标题】:Ember.js routes load order causing undefined errorEmber.js 路由加载顺序导致未定义的错误
【发布时间】:2013-07-14 22:41:55
【问题描述】:

我有一组路线。如果用户未通过身份验证,我想重定向一些路由,如果用户通过身份验证,我想重定向其他路由。

AuthenticatedRoute 案例正在运行,因此我为其他案例实现了以下路由:

App.NotAuthenticatedRoute = Ember.Route.extend
  beforeModel: (transition) ->
    if App.Auth.get('signedIn')
      return Ember.RSVP.reject();

  events: {
    error: (reason, transition) ->
      @transitionTo('home')
  }

App.RegistrationRoute = App.NotAuthenticatedRoute.extend
  setupController: (controller) ->
    controller.set('email', null)
    controller.set('password', null)
    controller.set('passwordConfirmation', null)

App.LoginRoute = App.NotAuthenticatedRoute.extend

每条路由都在我的 /routes 目录中它自己的文件中。 RegistrationRoute 完全按预期工作。但是,LoginRoute 会引发错误:

Uncaught TypeError: Cannot call method 'extend' of undefined login_route.js?body=1:2
(anonymous function)

我能想到的唯一原因是 LoginRouteNotAuthenticatedRoute 加载之前被解释。如果我将not_authenticated_route.js.coffee 中的NotAuthenticatedRoute 更改为anti_authenticated_route.js.coffee 中的AntiAuthenticatedRoute,一切正常。

我该如何处理加载顺序?

我还通过将打算继承​​的路由放在扩展目录中解决了这个问题,该目录在路由目录中的其他文件之前加载,这可能是解决问题的一个不错的解决方法。

【问题讨论】:

  • 您使用的是什么类型的构建系统,如果有的话?它接缝以相当字母 ASC 顺序加载 js 文件,这可能是如果您更改文件名它可以工作的原因
  • 我现在只是在使用 rails 资产管道。目前我不确定资产合规会对这个问题产生什么影响。
  • 看看guides.rubyonrails.org/asset_pipeline.html,我想这句话很有参考性:指令是从上到下处理的,但是require_tree包含文件的顺序是未指定的。您不应依赖其中的任何特定顺序。如果您需要确保连接文件中的某些特定 JavaScript 最终高于其他 JavaScript,请在​​清单中首先要求先决条件文件。请注意,require 指令系列可防止文件在输出中包含两次。 据说最终将预编译设置为 true 应该会有所帮助
  • 您可以通过查看生成的 .js 文件的顺序来验证顺序是否存在问题。

标签: routing ember.js load-order


【解决方案1】:

假设App.NotAuthenticatedRoutenot_authenticated_route.js 中定义,那么在App.LoginRoute 的定义中添加一个require 语句:

#= require not_authenticated_route

App.LoginRoute = App.NotAuthenticatedRoute.extend

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 2017-11-23
    • 2018-06-27
    • 2019-01-15
    相关资源
    最近更新 更多