public
Last active — forked from dmitry/backbone-mixin.js

Merge backbone views (mixin pattern)

  • Download Gist
backbone-mixin.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
define('backbone', ['lib-underscore', 'lib-backbone'], function () {
Backbone.mixin = function (view, mixin, custom) {
if (custom) {
if (custom.events && mixin.events) {
mixin = _.clone(mixin)
_.defaults(custom.events, mixin.events)
}
_.extend(mixin, custom)
}
var source = view.prototype || view
_.defaults(source, mixin)
if (mixin.events) {
if (source.events) {
_.defaults(source.events, mixin.events)
} else {
source.events = mixin.events
}
}
if (mixin.initialize !== undefined) {
var oldInitialize = source.initialize
source.initialize = function () {
mixin.initialize.apply(this, arguments)
oldInitialize.apply(this, arguments)
}
}
}
return Backbone
})
usage.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
* usage:
* Backboen.mixin(source, mixin, [custom])
*/
 
var FigureSection = Backbone.View.extend({
tagName: 'p'
, initialize: function(options) {
return
}
})
 
Backbone.mixin(FigureSection, FigureCaptionEditorMixin, {
onOpenEditCaption: function() {
this.openCaptionEditorHeight = this.$el.height()
}
, onUpdateCaption: function() {
if (this.openCaptionEditorHeight > this.$el.height()) {
this.$el.trigger('mouseleave')
}
}
})

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.