forked from mxriverlynn/backbone.modelbinding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.modelbinding.js
283 lines (248 loc) · 8.54 KB
/
backbone.modelbinding.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
// Backbone.ModelBinding v0.2.0
//
// Copyright (C)2011 Derick Bailey, Muted Solutions, LLC
// Distributed Under MIT Liscene
//
// Documentation and Full Licence Availabe at:
// http://github.com/derickbailey/backbone.modelbinding
// ----------------------------
// Backbone.ModelBinding
// ----------------------------
Backbone.ModelBinding = (function(){
function handleConventionBindings(view, model){
var conventions = Backbone.ModelBinding.Conventions;
for (var conventionName in conventions){
if (conventions.hasOwnProperty(conventionName)){
var conventionElement = conventions[conventionName];
var handler = conventionElement.handler;
var conventionSelector = conventionElement.selector;
handler.bind(conventionSelector, view, model);
}
}
}
return {
version: "0.2.0",
call: function(view, options){
Backbone.ModelBinding.Configuration.configureBindingAttributes(options);
handleConventionBindings(view, view.model);
Backbone.ModelBinding.Configuration.resetConfiguration();
}
}
})();
// ----------------------------
// Model Binding Configuration
// ----------------------------
Backbone.ModelBinding.Configuration = (function(){
var config = {
text: "id",
textarea: "id",
password: "id",
radio: "name",
checkbox: "id",
select: "id"
};
return {
configureBindingAttributes: function(options){
if (options) {
Backbone.ModelBinding._config = _.clone(config);
_.extend(config, options);
}
},
resetConfiguration: function(){
if (Backbone.ModelBinding._config) {
config = Backbone.ModelBinding._config;
delete Backbone.ModelBinding._config;
}
},
getBindingAttr: function(type){ return config[type]; },
getBindingValue: function(element, type){
var bindingAttr = Backbone.ModelBinding.Configuration.getBindingAttr(type);
return element.attr(bindingAttr);
}
}
})();
// ----------------------------
// Binding Conventions
// ----------------------------
Backbone.ModelBinding.Conventions = (function(){
function getElementType(element) {
var type = element[0].tagName.toLowerCase();
if (type == "input"){
type = element.attr("type");
}
return type;
}
var StandardInput = {
bind: function(selector, view, model){
view.$(selector).each(function(index){
var element = view.$(this);
var field = Backbone.ModelBinding.Configuration.getBindingValue(element, getElementType(element));
Backbone.ModelBinding.Binders.bidirectionalBinding.call(view, field, element, model);
});
}
};
var SelectBox = {
bind: function(selector, view, model){
view.$(selector).each(function(index){
var element = view.$(this);
var field = Backbone.ModelBinding.Configuration.getBindingValue(element, 'select');
Backbone.ModelBinding.Binders.bidirectionalSelectBinding.call(view, field, element, model);
});
}
};
var RadioGroup = {
bind: function(selector, view, model){
var self = this;
var foundElements = [];
view.$(selector).each(function(index){
var element = view.$(this);
var group_name = Backbone.ModelBinding.Configuration.getBindingValue(element, 'radio');
if (!foundElements[group_name]) {
foundElements[group_name] = true;
var bindingAttr = Backbone.ModelBinding.Configuration.getBindingAttr('radio');
Backbone.ModelBinding.Binders.bidirectionalRadioGroupBinding.call(view, group_name, model, bindingAttr);
}
});
}
};
var Checkbox = {
bind: function(selector, view, model){
var self = this;
view.$(selector).each(function(index){
var element = view.$(this);
var field = Backbone.ModelBinding.Configuration.getBindingValue(element, 'checkbox');
Backbone.ModelBinding.Binders.bidirectionalCheckboxBinding.call(view, field, element, model);
});
}
};
var DataBind = {
bind: function(selector, view, model){
view.$(selector).each(function(index){
var element = view.$(this);
var databind = element.attr("data-bind").split(" ");
var elementAttr = databind[0];
var modelAttr = databind[1];
model.bind("change:" + modelAttr, function(changedModel, val){
switch(elementAttr){
case "html":
element.html(val);
break;
case "text":
element.text(val);
break;
default:
element.attr(elementAttr, val);
}
});
});
}
};
return {
text: {selector: "input[type=text]", handler: StandardInput},
textarea: {selector: "textarea", handler: StandardInput},
password: {selector: "input[type=password]", handler: StandardInput},
radio: {selector: "input[type=radio]", handler: RadioGroup},
checkbox: {selector: "input[type=checkbox]", handler: Checkbox},
select: {selector: "select", handler: SelectBox},
databind: { selector: "*[data-bind]", handler: DataBind},
}
})();
// ----------------------------
// Bi-Directional Binding Methods
// ----------------------------
Backbone.ModelBinding.Binders = (function(){
var methods = {};
methods.bidirectionalBinding = function(attribute_name, element, model){
var self = this;
// bind the model changes to the form elements
model.bind("change:" + attribute_name, function(changed_model, val){
element.val(val);
});
// bind the form changes to the model
element.bind("change", function(ev){
var data = {};
data[attribute_name] = self.$(ev.target).val();
model.set(data);
});
// set the default value on the form, from the model
var attr_value = model.get(attribute_name);
if (attr_value) {
element.val(attr_value);
}
},
methods.bidirectionalSelectBinding = function(attribute_name, element, model){
var self = this;
// bind the model changes to the form elements
model.bind("change:" + attribute_name, function(changed_model, val){
element.val(val);
});
// bind the form changes to the model
element.bind("change", function(ev){
var data = {};
data[attribute_name] = self.$(ev.target).val();
data[attribute_name + "_text"] = self.$(":selected", ev.target).text();
model.set(data);
});
// set the default value on the form, from the model
var attr_value = model.get(attribute_name);
if (attr_value) {
element.val(attr_value);
}
},
methods.bidirectionalRadioGroupBinding = function(group_name, model, bindingAttr){
var self = this;
// bind the model changes to the form elements
model.bind("change:" + group_name, function(changed_model, val){
var value_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "][value=" + val + "]";
self.$(value_selector).attr("checked", "checked");
});
// bind the form changes to the model
var group_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "]";
self.$(group_selector).bind("change", function(ev){
var element = self.$(ev.currentTarget);
if (element.attr("checked")){
var data = {};
data[group_name] = element.val();
model.set(data);
}
});
// set the default value on the form, from the model
var attr_value = model.get(group_name);
if (attr_value) {
var value_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "][value=" + attr_value + "]";
self.$(value_selector).attr("checked", "checked");
}
},
methods.bidirectionalCheckboxBinding = function(attribute_name, element, model){
var self = this;
// bind the model changes to the form elements
model.bind("change:" + attribute_name, function(changed_model, val){
if (val){
element.attr("checked", "checked");
}
else{
element.removeAttr("checked");
}
});
// bind the form changes to the model
element.bind("change", function(ev){
var data = {};
var changedElement = self.$(ev.target);
var checked = changedElement.attr("checked")? true : false;
data[attribute_name] = checked;
model.set(data);
});
// set the default value on the form, from the model
var attr_exists = model.attributes.hasOwnProperty(attribute_name);
if (attr_exists) {
var attr_value = model.get(attribute_name);
if (attr_value){
element.attr("checked", "checked");
}
else{
element.removeAttr("checked");
}
}
}
return methods;
})();