forked from theironcook/Backbone.ModelBinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackbone.CollectionViewBinder.js
250 lines (188 loc) · 8.17 KB
/
Backbone.CollectionViewBinder.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
// Backbone.CollectionViewBinder v0.1.0
// (c) 2012 Bart Wood
// Distributed Under MIT License
(function(){
if(!Backbone){
throw 'Please include Backbone.js before Backbone.ModelBinder.js';
}
if(!Backbone.ModelBinder){
throw 'Please include Backbone.ModelBinder.js before Backbone.CollectionViewBinder.js';
}
Backbone.CollectionViewBinder = function(elManagerFactory){
_.bindAll(this);
this._elManagerFactory = elManagerFactory;
if(!this._elManagerFactory) throw 'elManagerFactory must be defined.';
// Let the factory just use the trigger function on the view binder
this._elManagerFactory.trigger = this.trigger;
};
Backbone.CollectionViewBinder.VERSION = '0.1.0';
_.extend(Backbone.CollectionViewBinder.prototype, Backbone.Events, {
createBoundEls: function(collection, parentEl){
this.unbind();
if(!collection) throw 'collection must be defined';
if(!parentEl) throw 'parentEl must be defined';
this._collection = collection;
this._elManagerFactory.setParentEl(parentEl);
this._collection.each(function(model){
this._onCollectionAdd(model);
}, this);
this._collection.on('add', this._onCollectionAdd, this);
this._collection.on('remove', this._onCollectionRemove, this);
this._collection.on('reset', this._onCollectionReset, 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._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){
var i, elManager, elManagers = _.values(this._elManagers);
for(i = 0; i < elManagers.length; i++){
elManager = elManagers[i];
if(elManager.getModel() === model){
return elManager;
}
}
return undefined;
},
_onCollectionAdd: function(model){
this._elManagers[model.cid] = this._elManagerFactory.makeElManager(model);
this._elManagers[model.cid].createEl();
},
_onCollectionRemove: function(model){
this._removeElManager(model);
},
_onCollectionReset: function(){
this._removeAllElManagers();
this._collection.each(function(model){
this._onCollectionAdd(model);
}, this);
},
_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];
}
}
});
// The DefaultElManagerFactory 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.CollectionViewBinder.ElManagerFactory = function(elHtml, bindings){
_.bindAll(this);
this._elHtml = elHtml;
this._bindings = bindings;
if(! _.isString(this._elHtml)) throw 'elHtml must be a valid html string';
};
_.extend(Backbone.CollectionViewBinder.ElManagerFactory.prototype, {
setParentEl: function(parentEl){
this._parentEl = parentEl;
},
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function(){
this._el = $(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 '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 ElManagerFactory 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.CollectionViewBinder.ViewManagerFactory = function(viewCreator){
_.bindAll(this);
this._viewCreator = viewCreator;
if(!_.isFunction(this._viewCreator)) throw 'viewCreator must be a valid function that accepts a model and returns a backbone view';
};
_.extend(Backbone.CollectionViewBinder.ViewManagerFactory.prototype, {
setParentEl: function(parentEl){
this._parentEl = parentEl;
},
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function(){
this._view = this._viewCreator(model);
$(this._parentEl).append(this._view.render(this._model).el);
this.trigger('elCreated', this._model, this._view);
},
removeEl: function(){
if(this._view.close !== undefined){
this._view.close();
}
else {
this._view.$el.remove();
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;
},
getEl: function(){
return this._view.el;
}
};
_.extend(elManager, this);
return elManager;
}
});
}).call(this);