From 84df4bf3bda67d63756b04cab21a11fb67bcf40e Mon Sep 17 00:00:00 2001 From: delambo Date: Thu, 17 Jan 2013 20:57:14 -0500 Subject: [PATCH] Support existing 'events' attribute as function #51 --- backbone.stickit.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/backbone.stickit.js b/backbone.stickit.js index 7618b49..7513889 100644 --- a/backbone.stickit.js +++ b/backbone.stickit.js @@ -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. @@ -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)) @@ -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) {