MarionetteJS (Backbone.Marionette) Playground

A base template to use for building Backbone and Marionette applications. http://marionettejs.com

by heuristocrat

HTML

<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script src="https://raw.github.com/marionettejs/backbone.wreqr/master/lib/backbone.wreqr.js"></script>
<script src="https://raw.github.com/marionettejs/backbone.eventbinder/master/lib/backbone.eventbinder.js"></script>
<script src="https://raw.github.com/marionettejs/backbone.marionette/master/lib/backbone.marionette.js"></script>

JavaScript

// HACK: This is not what I would consider the ideal way to add base-class
//       behavior, but I'm not sure how better to do this right now.
var origConstructor = Backbone.Marionette.View.prototype.constructor;
Backbone.Marionette.View.prototype.constructor = function(options) {
    if (options && options.mixins) {
        for (var mixin in options.mixins) {
            if (options.mixins.hasOwnProperty(mixin)) {
                if (this[mixin]) {
                    console.log('ERROR: trying to mixin "' + mixin + '", but it is already defined');
                }
                else {
                    this[mixin] = options.mixins[mixin];
                }
            }
        }
    }

    return origConstructor.apply(this, arguments);
};

// An alternate approach
Backbone.Marionette.View.prototype.mixinHelpers = function(helpers) {
    helpers = helpers || {};
    for (var helper in helpers) {
        if (helpers.hasOwnProperty(helper)) {
            if (this[helper]) {
                console.log('ERROR: trying to mixin helper "' + helper + '", but it is already defined');
            }
            else {
                this[helper] = helpers[helper];
            }
        }
    }
};