How do I correctly set up a Marionette EventAggregator as a Require.js module

backbone.js, marionette, requirejs

Solution

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);
});

Problem

I'm trying to setup a vent/EventAggregator as a separate Require.js module. I am using Marionette 1.0.2 (which I believe has a different implementation than legacy versions pre 1.0.0) with wreqr included: this code is from backbone.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, _); ``` When I set up my vent.js module what should it be? Something like this:- ``` define(['marionette'],function(Marionette){ return new Marionette.EventAggregator(); }) ``` Also in my require config should I explicitly be including backbone.wreqr.js or not? Or should just the marionette file (see snippet from above) be sufficient? For reference here is my 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); }); } ); ``` All help much appreciated! Much thanks, Sam _______**** UPDATE Thanks to Paul I figured out how to get my vent.js working. FYI I did NOT need to import the wreqr file seperately in the config. Here is the vent.js code:- ``` define(['backbone', 'marionette'],function(Backbone, Marionette){ return new Backbone.Wreqr.EventAggregator(); }); ```

Original source