-
-
Save hxgdzyuyi/3652964 to your computer and use it in GitHub Desktop.
Merge backbone views (mixin pattern)
This file contains 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
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 | |
}) |
This file contains 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
/* | |
* 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