如何正确设置Marionette EventAggregator作为Require.js模块

时间:2013-04-11 17:49:51

标签: backbone.js requirejs marionette

我正在尝试将vent / EventAggregator设置为单独的Require.js模块。我正在使用Marionette 1.0.2(我认为它与1.0.0之前的旧版本有不同的实现),其中包含wreqr:此代码来自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, _);

当我设置我的vent.js模块时应该是什么?这样的事情: -

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

同样在我的require配置中我应该明确地包括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();
});

1 个答案:

答案 0 :(得分:6)

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