Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #567 ... Adds a view. for the jQuery cached reference to a view…
…'s element ... and this.setElement() as a way to easily change it, redelegating events.
- Loading branch information
Showing
2 changed files
with
20 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -945,7 +945,7 @@ | |
// jQuery delegate for element lookup, scoped to DOM elements within the | ||
// current view. This should be prefered to global lookups where possible. | ||
$ : function(selector) { | ||
return (selector == null) ? $(this.el) : $(selector, this.el); | ||
return $(selector, this.el); | ||
}, | ||
|
||
// Initialize is an empty function by default. Override it with your own | ||
|
@@ -962,7 +962,7 @@ | |
// Remove this view from the DOM. Note that the view isn't present in the | ||
// DOM by default, so calling this method may be a no-op. | ||
remove : function() { | ||
$(this.el).remove(); | ||
this.$el.remove(); | ||
return this; | ||
}, | ||
|
||
|
@@ -978,6 +978,13 @@ | |
return el; | ||
}, | ||
|
||
setElement : function(element, delegate) { | ||
if (_.isString(element)) element = $(element)[0]; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tomasztunik
Contributor
|
||
this.el = element; | ||
this.$el = $(element); | ||
if (delegate !== false) this.delegateEvents(); | ||
}, | ||
|
||
// Set callbacks, where `this.events` is a hash of | ||
// | ||
// *{"event selector": "callback"}* | ||
|
@@ -1005,16 +1012,16 @@ | |
method = _.bind(method, this); | ||
eventName += '.delegateEvents' + this.cid; | ||
if (selector === '') { | ||
$(this.el).bind(eventName, method); | ||
this.$el.bind(eventName, method); | ||
} else { | ||
$(this.el).delegate(selector, eventName, method); | ||
this.$el.delegate(selector, eventName, method); | ||
} | ||
} | ||
}, | ||
|
||
// Clears all callbacks previously bound to the view with `delegateEvents`. | ||
undelegateEvents : function() { | ||
$(this.el).unbind('.delegateEvents' + this.cid); | ||
this.$el.unbind('.delegateEvents' + this.cid); | ||
}, | ||
|
||
// Performs the initial configuration of a View with a set of options. | ||
|
@@ -1038,9 +1045,9 @@ | |
var attrs = getValue(this, 'attributes') || {}; | ||
if (this.id) attrs.id = this.id; | ||
if (this.className) attrs['class'] = this.className; | ||
this.el = this.make(this.tagName, attrs); | ||
} else if (_.isString(this.el)) { | ||
this.el = $(this.el).get(0); | ||
this.setElement(this.make(this.tagName, attrs), false); | ||
} else { | ||
this.setElement(this.el, false); | ||
} | ||
} | ||
|
||
|
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
here you are creating jQuery object twice when you create it from the string
setElement : function(element, delegate) {
this.$el = $(element);
this.el = $(element)[0];
if (delegate !== false) this.delegateEvents();
}
should do as it will always first wrap normal dom in jQuery, convert string to jquery wrapped DOM etc and then just assign the DOM node to this.el + we dont need to do the isString check which is done in jQuery anyways