-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathBackbone.CollectionBinder.js
320 lines (248 loc) · 10.8 KB
/
Backbone.CollectionBinder.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Backbone.CollectionBinder v1.1.1
// (c) 2020 Bart Wood
// Distributed Under MIT License
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['underscore', 'jquery', 'backbone', 'Backbone.ModelBinder'], factory);
}
else if(typeof module !== 'undefined' && module.exports) {
// CommonJS
module.exports = factory(
require('underscore'),
require('jquery'),
require('backbone')
);
}
else {
// Browser globals
factory(_, $, Backbone);
}
}
(function(_, $, Backbone){
if(!Backbone){
throw new Error('Please include Backbone.js before Backbone.ModelBinder.js');
}
if(!Backbone.ModelBinder){
throw new Error('Please include Backbone.ModelBinder.js before Backbone.CollectionBinder.js');
}
Backbone.CollectionBinder = function(elManagerFactory, options){
_.bindAll.apply(_, [this].concat(_.functions(this)));
this._elManagers = {};
this._elManagerFactory = elManagerFactory;
if(!this._elManagerFactory) throw new Error('elManagerFactory must be defined.');
// Let the factory just use the trigger function on the view binder
this._elManagerFactory.trigger = this.trigger;
this._options = _.extend({}, Backbone.CollectionBinder.options, options);
};
// Static setter for class level options
Backbone.CollectionBinder.SetOptions = function(options){
Backbone.CollectionBinder.options = options;
};
Backbone.CollectionBinder.VERSION = '1.1.1';
_.extend(Backbone.CollectionBinder.prototype, Backbone.Events, {
bind: function(collection, parentEl){
this.unbind();
if(!collection) throw new Error('collection must be defined');
if(!parentEl) throw new Error('parentEl must be defined');
this._collection = collection;
this._elManagerFactory._setParentEl(parentEl);
this._onCollectionReset();
this._collection.on('add', this._onCollectionAdd, this);
this._collection.on('remove', this._onCollectionRemove, this);
this._collection.on('reset', this._onCollectionReset, this);
this._collection.on('sort', this._onCollectionSort, this);
},
unbind: function(){
if(this._collection !== undefined){
this._collection.off('add', this._onCollectionAdd);
this._collection.off('remove', this._onCollectionRemove);
this._collection.off('reset', this._onCollectionReset);
this._collection.off('sort', this._onCollectionSort);
}
this._removeAllElManagers();
},
getManagerForEl: function(el){
var i, elManager, elManagers = _.values(this._elManagers);
for(i = 0; i < elManagers.length; i++){
elManager = elManagers[i];
if(elManager.isElContained(el)){
return elManager;
}
}
return undefined;
},
getManagerForModel: function(model){
return this._elManagers[_.isObject(model)? model.cid : model];
},
_onCollectionAdd: function(model, collection, options){
var manager = this._elManagers[model.cid] = this._elManagerFactory.makeElManager(model);
manager.createEl();
var position = options && options.at;
if (this._options['autoSort'] && position != null && position < this._collection.length - 1) {
this._moveElToPosition(manager.getEl(), position);
}
},
_onCollectionRemove: function(model){
this._removeElManager(model);
},
_onCollectionReset: function(){
this._removeAllElManagers();
this._collection.each(function(model){
this._onCollectionAdd(model);
}, this);
this.trigger('elsReset', this._collection);
},
_onCollectionSort: function() {
if(this._options['autoSort']){
this.sortRootEls();
}
},
_removeAllElManagers: function(){
_.each(this._elManagers, function(elManager){
elManager.removeEl();
delete this._elManagers[elManager._model.cid];
}, this);
delete this._elManagers;
this._elManagers = {};
},
_removeElManager: function(model){
if(this._elManagers[model.cid] !== undefined){
this._elManagers[model.cid].removeEl();
delete this._elManagers[model.cid];
}
},
_moveElToPosition: function (modelEl, position) {
var nextModel = this._collection.at(position + 1);
if (!nextModel) return;
var nextManager = this.getManagerForModel(nextModel);
if (!nextManager) return;
var nextEl = nextManager.getEl();
if (!nextEl) return;
modelEl.detach();
modelEl.insertBefore(nextEl);
},
sortRootEls: function(){
this._collection.each(function(model, modelIndex){
var modelElManager = this.getManagerForModel(model);
if(modelElManager){
var modelEl = modelElManager.getEl();
var currentRootEls = $(this._elManagerFactory._getParentEl()).children();
if(currentRootEls[modelIndex] !== modelEl[0]){
modelEl.detach();
modelEl.insertBefore(currentRootEls[modelIndex]);
}
}
}, this);
}
});
// The ElManagerFactory is used for els that are just html templates
// elHtml - how the model's html will be rendered. Must have a single root element (div,span).
// bindings (optional) - either a string which is the binding attribute (name, id, data-name, etc.) or a normal bindings hash
Backbone.CollectionBinder.ElManagerFactory = function(elHtml, bindings){
_.bindAll.apply(_, [this].concat(_.functions(this)));
this._elHtml = elHtml;
this._bindings = bindings;
if(!_.isFunction(this._elHtml) && ! _.isString(this._elHtml)) throw new Error('elHtml must be a compliled template or an html string');
};
_.extend(Backbone.CollectionBinder.ElManagerFactory.prototype, {
_setParentEl: function(parentEl){
this._parentEl = parentEl;
},
_getParentEl: function(){
return this._parentEl;
},
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function(){
this._el = _.isFunction(this._elHtml) ? $(this._elHtml({model: this._model.toJSON()})) : $(this._elHtml);
$(this._parentEl).append(this._el);
if(this._bindings){
if(_.isString(this._bindings)){
this._modelBinder = new Backbone.ModelBinder();
this._modelBinder.bind(this._model, this._el, Backbone.ModelBinder.createDefaultBindings(this._el, this._bindings));
}
else if(_.isObject(this._bindings)){
this._modelBinder = new Backbone.ModelBinder();
this._modelBinder.bind(this._model, this._el, this._bindings);
}
else {
throw new Error('Unsupported bindings type, please use a boolean or a bindings hash');
}
}
this.trigger('elCreated', this._model, this._el);
},
removeEl: function(){
if(this._modelBinder !== undefined){
this._modelBinder.unbind();
}
this._el.remove();
this.trigger('elRemoved', this._model, this._el);
},
isElContained: function(findEl){
return this._el === findEl || $(this._el).has(findEl).length > 0;
},
getModel: function(){
return this._model;
},
getEl: function(){
return this._el;
}
};
_.extend(elManager, this);
return elManager;
}
});
// The ViewManagerFactory is used for els that are created and owned by backbone views.
// There is no bindings option because the view made by the viewCreator should take care of any binding
// viewCreator - a callback that will create backbone view instances for a model passed to the callback
Backbone.CollectionBinder.ViewManagerFactory = function(viewCreator){
_.bindAll.apply(_, [this].concat(_.functions(this)));
this._viewCreator = viewCreator;
if(!_.isFunction(this._viewCreator)) throw new Error('viewCreator must be a valid function that accepts a model and returns a backbone view');
};
_.extend(Backbone.CollectionBinder.ViewManagerFactory.prototype, {
_setParentEl: function(parentEl){
this._parentEl = parentEl;
},
_getParentEl: function(){
return this._parentEl;
},
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function(){
this._view = this._viewCreator(model);
this._view.render(this._model);
$(this._parentEl).append(this._view.el);
this.trigger('elCreated', this._model, this._view);
},
removeEl: function(){
if(this._view.close !== undefined){
this._view.close();
}
else {
this._view.$el.remove();
console && console.log && console.log('warning, you should implement a close() function for your view, you might end up with zombies');
}
this.trigger('elRemoved', this._model, this._view);
},
isElContained: function(findEl){
return this._view.el === findEl || this._view.$el.has(findEl).length > 0;
},
getModel: function(){
return this._model;
},
getView: function(){
return this._view;
},
getEl: function(){
return this._view.$el;
}
};
_.extend(elManager, this);
return elManager;
}
});
}));