Created
January 16, 2015 23:46
-
-
Save joshbedo/b916574332b6b1b58357 to your computer and use it in GitHub Desktop.
Mixins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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