Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unstickView() and unstickit() methods so that DOM events are unbound when repeatedly calling stickit() #67

Merged
merged 1 commit into from
Feb 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions backbone.stickit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
this._modelBindings = _.compact(this._modelBindings);
},

// Unbind all DOM -> model bindings
unstickView: function() {
this.$el.off(namespaceForView(this));
},

// Unbind all model -> view and view -> model events created by stickit
unstickit: function() {
this.unstickModel();
this.unstickView();
},

// Using `this.bindings` configuration or the `optionalBindingsConfig`, binds `this.model`
// or the `optionalModel` to elements in the view.
stickit: function(optionalModel, optionalBindingsConfig) {
Expand All @@ -50,7 +61,7 @@
bindings = optionalBindingsConfig || this.bindings || {};

this._modelBindings || (this._modelBindings = []);
this.unstickModel(model);
this.unstickit();

// Iterate through the selectors in the bindings configuration and configure
// the various options for each field.
Expand Down Expand Up @@ -117,8 +128,7 @@

// Wrap `view.remove` to unbind stickit model and dom events.
this.remove = _.wrap(this.remove, function(oldRemove) {
self.unstickModel();
self.$el.off(namespace);
self.unstickit();
if (oldRemove) oldRemove.call(self);
});
}
Expand All @@ -127,6 +137,11 @@
// Helpers
// -------

// Return a namespace for dom event handlers for a view
var namespaceForView = function(view) {
return '.stickit' + view.cid;
};

// Evaluates the given `path` (in object/dot-notation) relative to the given
// `obj`. If the path is null/undefined, then the given `obj` is returned.
var evaluatePath = function(obj, path) {
Expand Down