-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.dominus.js
133 lines (118 loc) · 5.38 KB
/
backbone.dominus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Backbone adapter for the dominus DOM manipulation framework.
// Structured to match the Backbone.NativeView method of mixins.
// This also includes Backbone.ajax functionality through Reqwest.
// More about dominus: https://github.com/bevacqua/dominus
// and Reqwest: https://github.com/ded/reqwest
//
// @author Eric Adams
// @copyright (c) 2015
//
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'dominus', 'reqwest'], factory);
}
else if (typeof exports === 'object') {
module.exports = factory(require('backbone'), require('dominus'), require('reqwest'));
}
else {
factory(Backbone, dominus, reqwest);
}
}(function (Backbone, dominus, reqwest) {
// Backbone.DomViewMixin is the view mixin used to integrate
// dominus-based views into your backbone application
// The mixin approach inspired by Backbone.NativeView
// See https://github.com/akre54/Backbone.NativeView#to-use
Backbone.DomViewMixin = {
// List of cached dom events for later removal
// hash of 'eventName.cid' => [listener1, listener2, listenerN]
_domEvents: null,
// Constructor, sets up the domEvents object
constructor: function() {
this._domEvents = {};
return Backbone.View.apply(this, arguments);
},
// View-scoped element lookup.
// Takes a string selector and returns an array-like object
// (i.e. an object with a numeric length property, like an
// NodeList, an Array, or a jQuery context) for easy iteration.
$: function(selector) {
return dominus.find(selector, this.el);
},
// Add a single event listener to the view's element (or a child element
// using `selector`). This only works for delegate-able events: not `focus`,
// `blur`, and not `change`, `submit`, and `reset` in Internet Explorer.
delegate: function(eventName, selector, listener) {
// Cache for later undelegation
var uniqEventName = eventName + '.' + this.cid;
if (!this._domEvents[uniqEventName]) {
this._domEvents[uniqEventName] = [];
}
this._domEvents[uniqEventName].push({selector: selector, listener: listener});
// Add the event to dominus
this.$el.on(eventName, selector, listener);
return this;
},
// A finer-grained `undelegateEvents` for removing a single delegated event.
// `selector` and `listener` are both optional.
undelegate: function(eventName, selector, listener) {
var item,
uniqEventName = eventName + '.' + this.cid;
if (this._domEvents[uniqEventName]) {
// Find any handlers in the event namespace
var handlers = this._domEvents[uniqEventName].slice();
var i = handlers.length;
while (i) {
i -= 1;
// Remove any events macthing the selector and listener
item = handlers[i];
if (item.selector === selector && item.listener === listener) {
this.$el.off(eventName, selector, listener);
this._domEvents[uniqEventName].splice(i, 1);
}
// Remove any event listeners for the eventName
else if (!listener) {
this.$el.off(eventName, (selector || item.selector), item.listener);
this._domEvents[uniqEventName].splice(i, 1);
}
}
}
return this;
},
// Clears all callbacks previously bound to the view by `delegateEvents`.
// 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() {
var item;
for (var uniqEventName in this._domEvents) {
if (this._domEvents.hasOwnProperty(uniqEventName)) {
var handlers = this._domEvents[uniqEventName].slice();
var eventName = uniqEventName.split('.')[0];
for (var i = 0, len = handlers.length; i < len; i+=1) {
item = handlers[i];
this.$el.off(eventName, item.selector, item.listener);
}
this._domEvents[uniqEventName] = null;
}
}
this._domEvents = {};
return this;
},
// Creates the `this.el` and `this.$el` references for this view using the
// given `el` and a hash of `attributes`. `el` can be a CSS selector or an
// HTML string, a jQuery context or an element. Subclasses can override
// this to utilize an alternative DOM manipulation API and are only required
// to set the `this.el` property.
_setElement: function(el) {
this.$el = dominus(el);
this.el = this.$el[0];
}
};
// Setup ajax functionality for backbone-sans-jquery
Backbone.ajax = function() {
return reqwest.compat.apply(reqwest, arguments);
};
// Set the Backbone.DomView constructor for your application
Backbone.DomView = Backbone.View.extend(Backbone.DomViewMixin);
return Backbone.DomView;
}));