Skip to content

Instantly share code, notes, and snippets.

@hxgdzyuyi
Forked from dmitry/backbone-mixin.js
Created September 6, 2012 08:21
Show Gist options
  • Save hxgdzyuyi/3652964 to your computer and use it in GitHub Desktop.
Save hxgdzyuyi/3652964 to your computer and use it in GitHub Desktop.
Merge backbone views (mixin pattern)
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:
* 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')
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment