Skip to content

Commit

Permalink
Support existing 'events' attribute as function #51
Browse files Browse the repository at this point in the history
  • Loading branch information
delambo committed Jan 18, 2013
1 parent f1d1cb1 commit 82fd274
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions backbone.stickit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@
props = ['autofocus', 'autoplay', 'async', 'checked', 'controls', 'defer', 'disabled', 'hidden', 'loop', 'multiple', 'open', 'readonly', 'required', 'scoped', 'selected'];

this._modelBindings || (this._modelBindings = []);

this.unstickModel(model);

// this.events may be a function, but we want to add new event bindings
// to it. Creating our own stickitEvents property allows us to add
// bindings while allowing this.events to remain a function. This also
// supports multiple calls to stickit() in a single Backbone View.
this.stickitEvents = _(_.result(this, 'events') || {}).extend(
this.stickitEvents
);
// Since `this.events` may be a function or hash, we'll create a stickitEvents
// property where we can mix in our own set of events. We also need to support
// multiple calls to `stickit()` in a single Backbone View.
this._stickitEvents = _(_.result(this, 'events') || {}).extend(this._stickitEvents);

// Iterate through the selectors in the bindings configuration and configure
// the various options for each field.
Expand Down Expand Up @@ -122,7 +118,7 @@
if (isFormEl($el) || isContenteditable($el)) {
// Bind events to the element which will update the model with changes.
_.each(config.eventsOverride || getModelEvents($el), function(type) {
self.stickitEvents[type+'.stickit '+selector] = function() {
self._stickitEvents[type+'.stickit '+selector] = function() {
var val = getElVal($el, isContenteditable($el));
// Don't update the model if false is returned from the `updateModel` configuration.
if (evaluateBoolean(self, config.updateModel, val, modelAttr))
Expand All @@ -144,8 +140,8 @@
}
});

// We added to `this.stickitEvents` so we need to re-delegate.
this.delegateEvents(this.stickitEvents);
// Have Backbone delegate any newly added events in `_stickitEvents`.
this.delegateEvents(this._stickitEvents);

// Wrap remove so that we can remove model events when this view is removed.
this.remove = _.wrap(this.remove, function(oldRemove) {
Expand Down

0 comments on commit 82fd274

Please sign in to comment.