【问题标题】:How do I correctly set up a Marionette EventAggregator as a Require.js module如何正确将 Marionette EventAggregator 设置为 Require.js 模块
【发布时间】:2013-04-11 17:49:50
【问题描述】:

我正在尝试将 vent/EventAggregator 设置为单独的 Require.js 模块。我正在使用包含 wreqr 的 Marionette 1.0.2(我相信它的实现与 1.0.0 之前的旧版本不同):此代码来自主干.marionette.js:-

        // Event Aggregator
// ----------------
// A pub-sub object that can be used to decouple various parts
// of an application through event-driven architecture.

Wreqr.EventAggregator = (function(Backbone, _){
  "use strict";
  var EA = function(){};

  // Copy the `extend` function used by Backbone's classes
  EA.extend = Backbone.Model.extend;

  // Copy the basic Backbone.Events on to the event aggregator
  _.extend(EA.prototype, Backbone.Events);

  return EA;
})(Backbone, _);

当我设置我的 vent.js 模块时,它应该是什么?像这样的东西:-

define(['marionette'],function(Marionette){
    return new Marionette.EventAggregator();
})

另外,在我的需求配置中,我是否应该明确包含骨干网.wreqr.js?还是只需要木偶文件(参见上面的 sn-p)就足够了?

这里是我的 app.js 供参考:-

require.config({

    paths : {
        backbone : 'lib/backbone',
        underscore : 'lib/underscore',
        jquery : 'lib/jquery',
        marionette : 'lib/backbone.marionette',
        'backbone.wreqr' : 'lib/backbone.wreqr',
        text : 'lib/text',
        templates : '../templates'

    },
    shim : {
        jquery : {
            exports : 'jQuery'
        },
        underscore : {
            exports : '_'
        },
        backbone : {
            deps : ['jquery', 'underscore'],
            exports : 'Backbone'
        },
        marionette : {
            deps : ['jquery', 'underscore', 'backbone'],
            exports : 'Marionette'
        },
        'backbone.wreqr' : {
            deps : ['backbone', 'marionette', 'underscore'],
            exports : 'Wreqr'
        }
    }
})

require(
    ["jquery",
        "underscore",
        "backbone",
        "marionette",
        "backbone.wreqr",
        "shell/shellapp"
    ],
    function($, _, Backbone, Marionette, Wreqr, ShellApp) {
        $(function() {
           //new ShellApp();
            var shell = ShellApp;
            shell.start();
            trace("shell: "+shell);
        });
    }
);

非常感谢所有帮助!

非常感谢,

山姆

_______****更新

感谢 Paul,我找到了让 vent.js 工作的方法。仅供参考,我不需要在配置中单独导入 wreqr 文件。这里是 vent.js 代码:-

define(['backbone', 'marionette'],function(Backbone, Marionette){
    return new Backbone.Wreqr.EventAggregator();
});

【问题讨论】:

  • github.com/marionettejs/backbone.marionette/wiki/… - 认为木偶文件应该足够了。
  • 感谢 Paul Grime。遗憾的是,当我按照教程进行操作时,只导入 Marionette(其中包含 Wreqr.EventAggregator)并将其用于发泄模块 - define(['marionette'],function(Marionette){ return new Marionette.EventAggregator(); }) 我在返回行上收到以下错误 - Uncaught TypeError: undefined is not a function。任何想法可能会发生什么?非常感谢,山姆。
  • console.log(Marionette, Marionette["EventAggregator"]) 在该模块中查看哪个是未定义的。然后检查EventAggregator 是否真的包含在marionette JS 文件中。事实上,这条源代码行 - github.com/marionettejs/backbone.marionette/blob/master/lib/… 表明 EventAggregator 被访问为 Backbone.Wreqr.EventAggregator
  • 谢谢保罗。当我在控制台中搜索 Marionette 对象时,Wreqr 不存在。正如您所说,它位于 Backbone 对象中,但是当我尝试在控制台上记录 Wreqr 对象时,我无法访问它。我可以在对象资源管理器中看到它,但是当我尝试使用 console.log(Backbone.Wreqr);或 console.log(Backbone["Wreqr"]);我的 vent.js 现在看起来像这样:-define(['backbone'],function(Backbone){ console.log(Backbone.Wreqr); }) 为什么我不能访问 Wreqr 对象有什么想法吗?再次感谢山姆
  • your marionette.js 文件是否具有与上面发布的链接相同的代码?它可能不是相同的版本/文件。即使是这样,也可能是单独加载 Wreqr 文件会破坏这一点。如果您的木偶版本确实有 Wreqr 字段,则删除 backbone.wreqr shim,否则它们可能会发生冲突。

标签: backbone.js requirejs marionette


【解决方案1】:

This works for me.

require.config({
    paths: {
        backbone: 'http://backbonejs.org/backbone',
        underscore: 'http://underscorejs.org/underscore',
        jquery: 'http://code.jquery.com/jquery-1.9.1',
        marionette: 'http://marionettejs.com/downloads/backbone.marionette'
    },
    shim: {
        jquery: {
            exports: 'jQuery'
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        marionette: {
            deps: ['jquery', 'underscore', 'backbone'],
            exports: 'Marionette'
        }
    }
});

require(["backbone", "marionette"], function (Backbone, Marionette) {
    console.log(Backbone.Wreqr.EventAggregator);
    var ea = new Backbone.Wreqr.EventAggregator();
    console.log(ea);
});

【讨论】:

  • 感谢 Paul,非常感谢您的时间和精力。当我偶然发现我的第一个 Marionette 应用程序时,这对我来说是一个巨大的帮助!
  • 我遇到了类似的问题,但我正在尝试使用 marionette 的核心 amd 构建并单独包含 wreqr 文件。尝试创建应用程序的新实例时出现 Backbone is not defined 错误。
猜你喜欢
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2021-08-30
相关资源
最近更新 更多