4

我正在尝试将 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();
})

同样在我的需求配置中,我是否应该明确包含backbone.wreqr.js?或者只是木偶文件(见上面的片段)就足够了?

这里是我的 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();
});
4

1 回答 1

6

这对我有用

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);
});
于 2013-04-12T10:01:05.787 回答