Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit

Permalink
Add View#useNative for view-specific jQuery usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Dec 16, 2013
1 parent a177a9f commit dac7500
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
13 changes: 8 additions & 5 deletions exoskeleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -1438,14 +1438,17 @@ var View = Backbone.View = function(options) {

// Set up all inheritable **Backbone.View** properties and methods.
_.extend(View.prototype, Events, {
// In case you want to include jQuery with your app
// for *some* views and use native methods for other views.
useNative: false,

// The default `tagName` of a View's element is `"div"`.
tagName: 'div',

// jQuery delegate for element lookup, scoped to DOM elements within the
// current view. This should be preferred to global lookups where possible.
$: function(selector) {
return Backbone.$ ? this.$el.find(selector) : this.findAll(selector);
return Backbone.$ && !this.useNative ? this.$el.find(selector) : this.findAll(selector);
},

// Exoskeleton-related DOM methods.
Expand All @@ -1472,7 +1475,7 @@ _.extend(View.prototype, Events, {
// applicable Backbone.Events listeners.
remove: function() {
var parent;
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
this.$el.remove();
} else if (parent = this.el.parentNode) {
parent.removeChild(this.el);
Expand All @@ -1484,7 +1487,7 @@ _.extend(View.prototype, Events, {
// Change the view's element (`this.el` property), including event
// re-delegation.
setElement: function(element, delegate) {
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
if (this.$el) this.undelegateEvents();
this.$el = element instanceof Backbone.$ ? element : Backbone.$(element);
this.el = this.$el[0];
Expand Down Expand Up @@ -1523,7 +1526,7 @@ _.extend(View.prototype, Events, {
var match = key.match(delegateEventSplitter);
var eventName = match[1], selector = match[2];

if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
eventName += '.delegateEvents' + this.cid;
method = method.bind(this);
this.$el.on(eventName, (selector ? selector : null), method);
Expand All @@ -1538,7 +1541,7 @@ _.extend(View.prototype, Events, {
// You usually don't need to use this, but may wish to if you have multiple
// Backbone views attached to the same DOM element.
undelegateEvents: function() {
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
this.$el.off('.delegateEvents' + this.cid);
} else {
utils.undelegate(this);
Expand Down
13 changes: 8 additions & 5 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ var View = Backbone.View = function(options) {

// Set up all inheritable **Backbone.View** properties and methods.
_.extend(View.prototype, Events, {
// In case you want to include jQuery with your app
// for *some* views and use native methods for other views.
useNative: false,

// The default `tagName` of a View's element is `"div"`.
tagName: 'div',

// jQuery delegate for element lookup, scoped to DOM elements within the
// current view. This should be preferred to global lookups where possible.
$: function(selector) {
return Backbone.$ ? this.$el.find(selector) : this.findAll(selector);
return Backbone.$ && !this.useNative ? this.$el.find(selector) : this.findAll(selector);
},

// Exoskeleton-related DOM methods.
Expand All @@ -68,7 +71,7 @@ _.extend(View.prototype, Events, {
// applicable Backbone.Events listeners.
remove: function() {
var parent;
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
this.$el.remove();
} else if (parent = this.el.parentNode) {
parent.removeChild(this.el);
Expand All @@ -80,7 +83,7 @@ _.extend(View.prototype, Events, {
// Change the view's element (`this.el` property), including event
// re-delegation.
setElement: function(element, delegate) {
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
if (this.$el) this.undelegateEvents();
this.$el = element instanceof Backbone.$ ? element : Backbone.$(element);
this.el = this.$el[0];
Expand Down Expand Up @@ -119,7 +122,7 @@ _.extend(View.prototype, Events, {
var match = key.match(delegateEventSplitter);
var eventName = match[1], selector = match[2];

if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
eventName += '.delegateEvents' + this.cid;
method = method.bind(this);
this.$el.on(eventName, (selector ? selector : null), method);
Expand All @@ -134,7 +137,7 @@ _.extend(View.prototype, Events, {
// You usually don't need to use this, but may wish to if you have multiple
// Backbone views attached to the same DOM element.
undelegateEvents: function() {
if (Backbone.$) {
if (Backbone.$ && !this.useNative) {
this.$el.off('.delegateEvents' + this.cid);
} else {
utils.undelegate(this);
Expand Down
24 changes: 24 additions & 0 deletions test/view-no-jq.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,28 @@
el.parentNode.removeChild(el);
});

test("useNative for view-specific native DOM usage", 1, function() {
var el = document.createElement("p");
el.innerHTML = '<a id="test"></a>';
document.body.appendChild(el);

Backbone.$ = {};
var View = Backbone.View.extend({
useNative: true
});

ok((function() {
try {
var view = new View;
view.$('p');
return true;
} catch(e) {
return false;
}
})())

Backbone.$ = undefined;
el.parentNode.removeChild(el);
});

})();

0 comments on commit dac7500

Please sign in to comment.