Skip to content

Instantly share code, notes, and snippets.

@joshbedo
Created January 16, 2015 23:46
Show Gist options
  • Save joshbedo/b916574332b6b1b58357 to your computer and use it in GitHub Desktop.
Save joshbedo/b916574332b6b1b58357 to your computer and use it in GitHub Desktop.
Mixins
var Class = function() {
this.initialize.apply(this, arguments);
};
_.extend(Class, {
extend: Backbone.View.extend,
inherit: function() {
var ret = this;
_.each(_.toArray(arguments), function(object) {
ret = ret.extend(object.prototype, object);
});
return ret;
},
include: function() {
return _.extend.apply(_, [this].concat(_.toArray(arguments)));
},
mixin: function() {
return _.extend.apply(_, [this.prototype].concat(_.toArray(arguments)));
}
});
Class.mixin(Backbone.Events, {
initialize: function() {}
});
var Foo = Class.inherit(Backbone.View, Backbone.Router).extend({
// ...
});
Foo.include({ bar: true });
Foo.mixin({ baz: true });
var foo = new Foo();
console.log(foo);
console.log('Instance of Class :', foo instanceof Class);
console.log('Instance of View :', foo instanceof Backbone.View);
console.log('Instance of Router :', foo instanceof Backbone.Router);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment