【问题标题】:Uncaught TypeError: Cannot read property 'EventAggregator' of undefined (Backbone, Marionette, RequireJS)未捕获的类型错误:无法读取未定义的属性“EventAggregator”(Backbone、Marionette、RequireJS)
【发布时间】:2012-10-05 18:23:45
【问题描述】:

使用 Backbone.Marionette 创建一个新应用程序,当我运行 Express 应用程序并加载页面时,我在控制台中收到错误:

Uncaught TypeError: Cannot read property 'EventAggregator' of undefined 
backbone.marionette.js:1504

显示它在实际的marionette 库中。我看过那行:

Marionette.EventAggregator = Backbone.Wreqr.EventAggregator;

我认为wreqr 可能是我必须添加的额外库?

这是创建应用程序的代码:

require([
    'jquery', 
    'underscore', 
    'backbone', 
    'marionette'
], function( $, _, Backbone, Marionette ){
    MyApp = new Backbone.Marionette.Application();

    MyApp.addRegions({
        main_region: '#main_region'
    });

    MyApp.addInitializer( function(options) {
        var login_form_view = new LoginFormView();
    });
});

以及设置库位置的要求配置:

// using RequireJS 1.0.7
    require.config({
        paths: {
            '$': 'libs/jquery-1.8.2-min',
            'underscore': 'libs/underscore-min', // AMD support
            'backbone': 'libs/backbone.min', // AMD support
            'bootstrap' : 'libs/bootstrap.min',
            'marionette' : 'libs/backbone.marionette', 
            'wreqr' : 'libs/backbone.wreqr',
            'templates': '../templates',
            'text': 'libs/require/text', 
            'login': 'views/user/login'
        }
    });

有人知道是什么原因造成的错误吗?

【问题讨论】:

    标签: javascript backbone.js requirejs underscore.js marionette


    【解决方案1】:

    是的,wreqr 是 Marionette 的依赖项。

    您已指定 Wreqr 的路径,但您也需要加载它。在加载 Marionette 之前。

    require([
         'jquery', 
         'underscore', 
         'backbone', 
         'wreqr',
         'marionette'
    ], function( $, _, Backbone, Marionette ){
    MyApp = new Backbone.Marionette.Application();
    
    MyApp.addRegions({
        main_region: '#main_region'
    });
    
    MyApp.addInitializer( function(options) {
        var login_form_view = new LoginFormView();
    });
    

    });

    【讨论】:

    • 谢谢,我会试一试并报告。一秒。
    • 成功!还必须添加 EventBinder:github.com/marionettejs/backbone.eventbinder
    • 仅供参考:在 v1.0.0-beta2 中,我这样做是为了让“backbone.marionette.js”文件在其中包含 Wreqr 和 EventBinder。不过,如果需要,您可以下载没有依赖项的“核心”marionette.js 文件。 marionettejs.com#downloads
    猜你喜欢
    • 2014-07-12
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多