【问题标题】:Ember causes a routing issue only when I use a Mixin. How do I resolve this?仅当我使用 Mixin 时,Ember 才会导致路由问题。我该如何解决这个问题?
【发布时间】:2014-11-05 03:37:21
【问题描述】:

在我的 Ember CLI 应用程序中,我有一个名为 insights 的路由。该路由扩展了 check-user mixin。

/routes/insights.js:

import Ember from 'ember';
import CheckUser from 'client-web/mixins/check-user';

export default Ember.Route.extend(CheckUser, {
setupController: function(controller, model) {
    var _this = this;

    if (this.get('userAuthenticated') === true) {
        // Do some stuff here
    } else {
        _this.transitionToRoute('sign-in');
    }
},

getToday: function() {
    var today   = new Date(),
        dd      = today.getDate(),
        mm      = today.getMonth() + 1,
        yyyy    = today.getFullYear();

    if (dd < 10) dd = '0' + dd;
    if (mm < 10) mm = '0' + mm;

    today = mm + '/' + dd + '/' + yyyy;

    return today;
},

getTomorrow: function() {
    var toDate  = new Date(this.getToday());
    toDate.setDate(toDate.getDate() + 1);

    var day         = toDate.getDate(),
        month       = toDate.getMonth() + 1,
        year        = toDate.getFullYear();

    if (day < 10) day = '0' + day;
    if (month < 10) month = '0' + month;

    var tomorrow = month + '/' + day + '/' + year;

    return tomorrow;
}
});

当我使用 this.transitionTo('insights') 从另一个路由转换到该路由或手动将页面刷新到 myapplication.com/insights 时,一切正常,此路由呈现我指定的模板。但是,我的模板中有一些链接使用 {{link-to}} 帮助程序链接到见解路线。当用户单击其中一个链接时,我收到以下错误:

Error while processing route: insights undefined is not a function TypeError: undefined is not a function
at __exports__.default.Ember.Route.extend.setupController (client-web/routes/insights.js:15:14)
at apply (http://localhost:3000/assets/vendor.js:21144:27)
at superWrapper [as setupController] (http://localhost:3000/assets/vendor.js:20721:15)
at EmberObject.extend.setup (http://localhost:3000/assets/vendor.js:51254:18)
at handlerEnteredOrUpdated (http://localhost:3000/assets/vendor.js:54266:36)
at http://localhost:3000/assets/vendor.js:54235:18
at forEach (http://localhost:3000/assets/vendor.js:55303:54)
at setupContexts (http://localhost:3000/assets/vendor.js:54234:9)
at finalizeTransition (http://localhost:3000/assets/vendor.js:54404:9)
at http://localhost:3000/assets/vendor.js:53954:20 

第 15 行是 getToday 函数。不知道为什么只有当我使用链接到帮助程序时它才会在那里中断。我错过了什么?

【问题讨论】:

    标签: javascript ember.js ember-cli


    【解决方案1】:

    transitionToRoute 是一个控制器方法。路由上的方法是transitionTo

    我认为您的行号可能由于转译而关闭。您可以从堆栈跟踪中看到错误正在setupController 中引发。顺便说一句,如果你定义像setupController: function Insights$setupController() ... 这样的函数,你会得到更好的跟踪。

    要点:首先,我不知道您为什么要费心定义_this。二、不要在setupController做这种检查; beforeModel 会更好。第三,我不是多余的=== true 的忠实粉丝。

    查看修复意见 本质上,将 setupController 更改为 beforeModel 修复了该错误。

    【讨论】:

    • 我声明 _this 来处理 if 语句中的一些东西,它说'//在这里做一些东西'。由于 jQuery,_this 是必需的。更改了过渡到。谢谢你。
    • @Nagarjun 解决了问题吗?
    • 将 setupController 更改为 beforeModel 似乎已经修复了该错误,但它引入了另一个错误。现在,我的脚本正在呈现登录路由,因为 this.get('userAuthenticated') 的值似乎是假的。记住 userAuthenticated 是 Mixin 检查用户内部的一个属性。在另一个也使用该 mixin 的页面上,userAuthenticated 的值设置为 true。当有人使用链接到帮助器时,不要混合属性值吗?
    • 好吧,路由被实例化一次,然后mixin被包含到实例中。任何“transitionTo”(或{{link-to}})都会调用路由器的一个实例。问题是 userAuthenticated 属性何时由 mixin 设置。您可能想深入研究它并查看。可能在调用“beforeModel”时尚未设置它。顺便说一句,当使用这样的mixin时,你可能需要在这里和那里调用_super,否则永远不会调用mixin中的同名钩子。
    • userAuthenticated 在 mixin 的 init 函数中设置,因此根据我的 console.log()s 判断,它看起来像是在 beforeModel 之前运行。我确实设法解决了这个问题。我更改了 Mixin 内部的逻辑,在 beforeModel 之前设置了 userAuthenticated 属性,现在它可以工作了。谢谢!
    猜你喜欢
    • 2020-03-25
    • 2020-02-14
    • 2021-10-23
    • 1970-01-01
    • 2021-07-31
    • 2020-12-28
    相关资源
    最近更新 更多