appetizer-ui 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/appetizer-ui.gemspec +15 -13
- data/lib/appetizer/ui/assets.rb +21 -8
- data/lib/appetizer/ui/jasmine/js/jquery-jasmine.js +288 -0
- data/lib/appetizer/ui/jasmine/js/spec-runner.coffee +0 -1
- data/lib/appetizer/ui/jasmine/views/specs.erb +12 -4
- data/lib/appetizer/ui/rake.rb +29 -10
- data/lib/appetizer/ui/spec.rb +1 -2
- metadata +122 -61
- data/lib/appetizer/ui/app/js/appetizer.coffee +0 -15
- data/lib/appetizer/ui/app/js/appetizer/core.coffee +0 -20
- data/lib/appetizer/ui/app/js/appetizer/model.coffee +0 -1
- data/lib/appetizer/ui/app/js/appetizer/router.coffee +0 -70
- data/lib/appetizer/ui/app/js/appetizer/view.coffee +0 -139
- data/lib/appetizer/ui/app/js/appetizer/xdr.coffee +0 -57
- data/lib/appetizer/ui/app/views/client/appetizer/missing.jst.eco +0 -3
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/appetizerExtensions.spec.js +0 -54
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/checkboxConventionBindings.spec.js +0 -110
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/configurableBindingAttributes.spec.js +0 -117
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/configureAllBindingAttributes.spec.js +0 -139
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/customConvention.spec.js +0 -53
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/dataBindConvention.spec.js +0 -151
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/dataBindMultiple.spec.js +0 -36
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/dataBindSubstitutions.spec.js +0 -137
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/globalConfiguraAllBindingAttributes.spec.js +0 -124
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/globalConfigurableBindingAttributes.spec.js +0 -36
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/helpers/SpecHelper.js +0 -4
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/helpers/sample.backbone.app.js +0 -159
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/html5inputConventionBinding.spec.js +0 -142
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/modelUnbinding.spec.js +0 -73
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/noConflict.spec.js +0 -36
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/radioButtonConventionBinding.spec.js +0 -41
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/selectboxConventionBindings.spec.js +0 -60
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/textareaConventionBinding.spec.js +0 -29
- data/lib/appetizer/ui/jasmine/js/backbone.modelbinding/textboxConventionBinding.spec.js +0 -66
- data/lib/appetizer/ui/vendor/js/backbone.js +0 -1290
- data/lib/appetizer/ui/vendor/js/backbone.modelbinding.js +0 -624
- data/lib/appetizer/ui/vendor/js/jquery.js +0 -9266
- data/lib/appetizer/ui/vendor/js/json2.js +0 -480
- data/lib/appetizer/ui/vendor/js/underscore.js +0 -999
- data/lib/appetizer/ui/vendor/js/underscore.string.js +0 -480
@@ -1,624 +0,0 @@
|
|
1
|
-
// Backbone.ModelBinding v0.4.3
|
2
|
-
// [jbarnette] Added getAttributeValue, allowing values to come from functions.
|
3
|
-
// [cmorse] Added mediate support
|
4
|
-
// [toots] Added foo.bar.baz syntax for bound attributes
|
5
|
-
//
|
6
|
-
// Copyright (C)2011 Derick Bailey, Muted Solutions, LLC
|
7
|
-
// Distributed Under MIT Liscene
|
8
|
-
//
|
9
|
-
// Documentation and Full Licence Availabe at:
|
10
|
-
// http://github.com/derickbailey/backbone.modelbinding
|
11
|
-
|
12
|
-
// ----------------------------
|
13
|
-
// Backbone.ModelBinding
|
14
|
-
// ----------------------------
|
15
|
-
|
16
|
-
;(function(root){
|
17
|
-
|
18
|
-
var modelbinding = (function(Backbone, _, $) {
|
19
|
-
var modelBinding = {
|
20
|
-
version: "0.4.3",
|
21
|
-
|
22
|
-
bind: function(view, options){
|
23
|
-
view.modelBinder = new ModelBinder(view, options);
|
24
|
-
view.modelBinder.bind();
|
25
|
-
},
|
26
|
-
|
27
|
-
unbind: function(view){
|
28
|
-
if (view.modelBinder){
|
29
|
-
view.modelBinder.unbind()
|
30
|
-
}
|
31
|
-
}
|
32
|
-
};
|
33
|
-
|
34
|
-
// pop attribute name from model. It takes, in order
|
35
|
-
// of priority:
|
36
|
-
// model[name] if defined and not a function
|
37
|
-
// model[name]() if defined and a function
|
38
|
-
// model.get(name) otherwise
|
39
|
-
var popAttribute = function (model, name) {
|
40
|
-
var value = model[name];
|
41
|
-
if (value) {
|
42
|
-
if (_.isFunction(value))
|
43
|
-
value = value.apply(model)
|
44
|
-
} else {
|
45
|
-
if (model.get) {
|
46
|
-
value = model.get(name);
|
47
|
-
} else {
|
48
|
-
return undefined;
|
49
|
-
}
|
50
|
-
}
|
51
|
-
return value;
|
52
|
-
}
|
53
|
-
|
54
|
-
var getAttributeValue = function(model, name) {
|
55
|
-
var value = model;
|
56
|
-
|
57
|
-
name = "" + name;
|
58
|
-
|
59
|
-
names = name.split(".");
|
60
|
-
|
61
|
-
_.each(names, function (name) {
|
62
|
-
value = popAttribute(value, name);
|
63
|
-
});
|
64
|
-
|
65
|
-
return value;
|
66
|
-
};
|
67
|
-
|
68
|
-
var ModelBinder = function(view, options){
|
69
|
-
this.config = new modelBinding.Configuration(options);
|
70
|
-
this.modelBindings = [];
|
71
|
-
this.elementBindings = [];
|
72
|
-
|
73
|
-
this.bind = function(){
|
74
|
-
var conventions = modelBinding.Conventions;
|
75
|
-
for (var conventionName in conventions){
|
76
|
-
if (conventions.hasOwnProperty(conventionName)){
|
77
|
-
var conventionElement = conventions[conventionName];
|
78
|
-
var handler = conventionElement.handler;
|
79
|
-
var conventionSelector = conventionElement.selector;
|
80
|
-
handler.bind.call(this, conventionSelector, view, view.model, this.config);
|
81
|
-
}
|
82
|
-
}
|
83
|
-
};
|
84
|
-
|
85
|
-
this.unbind = function(){
|
86
|
-
// unbind the html element bindings
|
87
|
-
_.each(this.elementBindings, function(binding){
|
88
|
-
binding.element.unbind(binding.eventName, binding.callback);
|
89
|
-
});
|
90
|
-
|
91
|
-
// unbind the model bindings
|
92
|
-
_.each(this.modelBindings, function(binding){
|
93
|
-
binding.model.unbind(binding.eventName, binding.callback);
|
94
|
-
});
|
95
|
-
};
|
96
|
-
|
97
|
-
this.registerModelBinding = function(model, attrName, callback){
|
98
|
-
// bind the model changes to the form elements
|
99
|
-
var eventName = "change:" + attrName;
|
100
|
-
model.bind(eventName, callback);
|
101
|
-
this.modelBindings.push({model: model, eventName: eventName, callback: callback});
|
102
|
-
};
|
103
|
-
|
104
|
-
this.registerDataBinding = function(model, eventName, callback){
|
105
|
-
// bind the model changes to the elements
|
106
|
-
|
107
|
-
model.bind(eventName, callback);
|
108
|
-
this.modelBindings.push({model: model, eventName: eventName, callback: callback});
|
109
|
-
};
|
110
|
-
|
111
|
-
this.registerElementBinding = function(element, callback){
|
112
|
-
// bind the form changes to the model
|
113
|
-
element.bind("change", callback);
|
114
|
-
this.elementBindings.push({element: element, eventName: "change", callback: callback});
|
115
|
-
};
|
116
|
-
};
|
117
|
-
|
118
|
-
// ----------------------------
|
119
|
-
// Model Binding Configuration
|
120
|
-
// ----------------------------
|
121
|
-
modelBinding.Configuration = function(options){
|
122
|
-
this.bindingAttrConfig = {};
|
123
|
-
|
124
|
-
_.extend(this.bindingAttrConfig,
|
125
|
-
modelBinding.Configuration.bindindAttrConfig,
|
126
|
-
options
|
127
|
-
);
|
128
|
-
|
129
|
-
if (this.bindingAttrConfig.all){
|
130
|
-
var attr = this.bindingAttrConfig.all;
|
131
|
-
delete this.bindingAttrConfig.all;
|
132
|
-
for (var inputType in this.bindingAttrConfig){
|
133
|
-
if (this.bindingAttrConfig.hasOwnProperty(inputType)){
|
134
|
-
this.bindingAttrConfig[inputType] = attr;
|
135
|
-
}
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
this.getBindingAttr = function(type){
|
140
|
-
return this.bindingAttrConfig[type];
|
141
|
-
};
|
142
|
-
|
143
|
-
this.getBindingValue = function(element, type){
|
144
|
-
var bindingAttr = this.getBindingAttr(type);
|
145
|
-
return element.attr(bindingAttr);
|
146
|
-
};
|
147
|
-
|
148
|
-
};
|
149
|
-
|
150
|
-
modelBinding.Configuration.bindindAttrConfig = {
|
151
|
-
text: "id",
|
152
|
-
textarea: "id",
|
153
|
-
password: "id",
|
154
|
-
radio: "name",
|
155
|
-
checkbox: "id",
|
156
|
-
select: "id",
|
157
|
-
number: "id",
|
158
|
-
range: "id",
|
159
|
-
tel: "id",
|
160
|
-
search: "id",
|
161
|
-
url: "id",
|
162
|
-
email: "id"
|
163
|
-
};
|
164
|
-
|
165
|
-
modelBinding.Configuration.store = function(){
|
166
|
-
modelBinding.Configuration.originalConfig = _.clone(modelBinding.Configuration.bindindAttrConfig);
|
167
|
-
};
|
168
|
-
|
169
|
-
modelBinding.Configuration.restore = function(){
|
170
|
-
modelBinding.Configuration.bindindAttrConfig = modelBinding.Configuration.originalConfig;
|
171
|
-
};
|
172
|
-
|
173
|
-
modelBinding.Configuration.configureBindingAttributes = function(options){
|
174
|
-
if (options.all){
|
175
|
-
this.configureAllBindingAttributes(options.all);
|
176
|
-
delete options.all;
|
177
|
-
}
|
178
|
-
_.extend(modelBinding.Configuration.bindindAttrConfig, options);
|
179
|
-
};
|
180
|
-
|
181
|
-
modelBinding.Configuration.configureAllBindingAttributes = function(attribute){
|
182
|
-
var config = modelBinding.Configuration.bindindAttrConfig;
|
183
|
-
config.text = attribute;
|
184
|
-
config.textarea = attribute;
|
185
|
-
config.password = attribute;
|
186
|
-
config.radio = attribute;
|
187
|
-
config.checkbox = attribute;
|
188
|
-
config.select = attribute;
|
189
|
-
config.number = attribute;
|
190
|
-
config.range = attribute;
|
191
|
-
config.tel = attribute;
|
192
|
-
config.search = attribute;
|
193
|
-
config.url = attribute;
|
194
|
-
config.email = attribute;
|
195
|
-
};
|
196
|
-
|
197
|
-
// ----------------------------
|
198
|
-
// Text, Textarea, and Password Bi-Directional Binding Methods
|
199
|
-
// ----------------------------
|
200
|
-
var StandardBinding = (function(Backbone){
|
201
|
-
var methods = {};
|
202
|
-
|
203
|
-
var _getElementType = function(element) {
|
204
|
-
var type = element[0].tagName.toLowerCase();
|
205
|
-
if (type == "input"){
|
206
|
-
type = element.attr("type");
|
207
|
-
if (type == undefined || type == ''){
|
208
|
-
type = 'text';
|
209
|
-
}
|
210
|
-
}
|
211
|
-
return type;
|
212
|
-
};
|
213
|
-
|
214
|
-
methods.bind = function(selector, view, model, config){
|
215
|
-
var modelBinder = this;
|
216
|
-
|
217
|
-
view.$(selector).each(function(index){
|
218
|
-
var element = view.$(this);
|
219
|
-
var elementType = _getElementType(element);
|
220
|
-
var attribute_name = config.getBindingValue(element, elementType);
|
221
|
-
if (!attribute_name) return;
|
222
|
-
var mediator = modelBinding.getMediator(element);
|
223
|
-
|
224
|
-
var modelChange = function(changed_model, val) {
|
225
|
-
element.val(mediator.render(val));
|
226
|
-
};
|
227
|
-
|
228
|
-
var popData = function (options, name) {
|
229
|
-
var target;
|
230
|
-
if (target = options.model[name]) {
|
231
|
-
options.model = _.isFunction(target) ? target() : target
|
232
|
-
} else {
|
233
|
-
options.data[name] = options.model.get(name) || {};
|
234
|
-
options.data = options.data[name];
|
235
|
-
}
|
236
|
-
}
|
237
|
-
|
238
|
-
var setModelValue = function(attr_name, value){
|
239
|
-
var origData = {};
|
240
|
-
var options = { data: origData, model: model };
|
241
|
-
|
242
|
-
attr_names = attr_name.split(".");
|
243
|
-
attr_name = attr_names.pop();
|
244
|
-
|
245
|
-
_.each(attr_names, function (name) {
|
246
|
-
popData(options, name);
|
247
|
-
});
|
248
|
-
|
249
|
-
options.data[attr_name] = mediator.parse(value);
|
250
|
-
options.model.set(origData);
|
251
|
-
};
|
252
|
-
|
253
|
-
var elementChange = function(ev){
|
254
|
-
setModelValue(attribute_name, view.$(ev.target).val());
|
255
|
-
};
|
256
|
-
|
257
|
-
modelBinder.registerModelBinding(model, attribute_name, modelChange);
|
258
|
-
modelBinder.registerElementBinding(element, elementChange);
|
259
|
-
|
260
|
-
// set the default value on the form, from the model
|
261
|
-
var attr_value = getAttributeValue(model, attribute_name);
|
262
|
-
if (typeof attr_value !== "undefined" && attr_value !== null) {
|
263
|
-
element.val(mediator.render(attr_value));
|
264
|
-
} else {
|
265
|
-
var elVal = element.val();
|
266
|
-
if (elVal){
|
267
|
-
setModelValue(attribute_name, elVal);
|
268
|
-
}
|
269
|
-
}
|
270
|
-
});
|
271
|
-
};
|
272
|
-
|
273
|
-
return methods;
|
274
|
-
})(Backbone);
|
275
|
-
|
276
|
-
// ----------------------------
|
277
|
-
// Select Box Bi-Directional Binding Methods
|
278
|
-
// ----------------------------
|
279
|
-
var SelectBoxBinding = (function(Backbone){
|
280
|
-
var methods = {};
|
281
|
-
|
282
|
-
methods.bind = function(selector, view, model, config){
|
283
|
-
var modelBinder = this;
|
284
|
-
|
285
|
-
view.$(selector).each(function(index){
|
286
|
-
var element = view.$(this);
|
287
|
-
var attribute_name = config.getBindingValue(element, 'select');
|
288
|
-
if (!attribute_name) return;
|
289
|
-
|
290
|
-
var modelChange = function(changed_model, val){ element.val(val); };
|
291
|
-
|
292
|
-
var setModelValue = function(attr, val, text){
|
293
|
-
var data = {};
|
294
|
-
data[attr] = val;
|
295
|
-
data[attr + "_text"] = text;
|
296
|
-
model.set(data);
|
297
|
-
};
|
298
|
-
|
299
|
-
var elementChange = function(ev){
|
300
|
-
var targetEl = view.$(ev.target);
|
301
|
-
var value = targetEl.val();
|
302
|
-
var text = targetEl.find(":selected").text();
|
303
|
-
setModelValue(attribute_name, value, text);
|
304
|
-
};
|
305
|
-
|
306
|
-
modelBinder.registerModelBinding(model, attribute_name, modelChange);
|
307
|
-
modelBinder.registerElementBinding(element, elementChange);
|
308
|
-
|
309
|
-
// set the default value on the form, from the model
|
310
|
-
var attr_value = getAttributeValue(model, attribute_name);
|
311
|
-
if (typeof attr_value !== "undefined" && attr_value !== null) {
|
312
|
-
element.val(attr_value);
|
313
|
-
}
|
314
|
-
|
315
|
-
// set the model to the form's value if there is no model value
|
316
|
-
if (element.val() != attr_value) {
|
317
|
-
var value = element.val();
|
318
|
-
var text = element.find(":selected").text();
|
319
|
-
setModelValue(attribute_name, value, text);
|
320
|
-
}
|
321
|
-
});
|
322
|
-
};
|
323
|
-
|
324
|
-
return methods;
|
325
|
-
})(Backbone);
|
326
|
-
|
327
|
-
// ----------------------------
|
328
|
-
// Radio Button Group Bi-Directional Binding Methods
|
329
|
-
// ----------------------------
|
330
|
-
var RadioGroupBinding = (function(Backbone){
|
331
|
-
var methods = {};
|
332
|
-
|
333
|
-
methods.bind = function(selector, view, model, config){
|
334
|
-
var modelBinder = this;
|
335
|
-
|
336
|
-
var foundElements = [];
|
337
|
-
view.$(selector).each(function(index){
|
338
|
-
var element = view.$(this);
|
339
|
-
|
340
|
-
var group_name = config.getBindingValue(element, 'radio');
|
341
|
-
if (!foundElements[group_name]) {
|
342
|
-
foundElements[group_name] = true;
|
343
|
-
var bindingAttr = config.getBindingAttr('radio');
|
344
|
-
|
345
|
-
var modelChange = function(model, val){
|
346
|
-
var value_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "][value='" + val + "']";
|
347
|
-
view.$(value_selector).attr("checked", "checked");
|
348
|
-
};
|
349
|
-
modelBinder.registerModelBinding(model, group_name, modelChange);
|
350
|
-
|
351
|
-
var setModelValue = function(attr, val){
|
352
|
-
var data = {};
|
353
|
-
data[attr] = val;
|
354
|
-
model.set(data);
|
355
|
-
};
|
356
|
-
|
357
|
-
// bind the form changes to the model
|
358
|
-
var elementChange = function(ev){
|
359
|
-
var element = view.$(ev.currentTarget);
|
360
|
-
if (element.is(":checked")){
|
361
|
-
setModelValue(group_name, element.val());
|
362
|
-
}
|
363
|
-
};
|
364
|
-
|
365
|
-
var group_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "]";
|
366
|
-
view.$(group_selector).each(function(){
|
367
|
-
var groupEl = $(this);
|
368
|
-
modelBinder.registerElementBinding(groupEl, elementChange);
|
369
|
-
});
|
370
|
-
|
371
|
-
var attr_value = getAttributeValue(model, group_name);
|
372
|
-
if (typeof attr_value !== "undefined" && attr_value !== null) {
|
373
|
-
// set the default value on the form, from the model
|
374
|
-
var value_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "][value='" + attr_value + "']";
|
375
|
-
view.$(value_selector).attr("checked", "checked");
|
376
|
-
} else {
|
377
|
-
// set the model to the currently selected radio button
|
378
|
-
var value_selector = "input[type=radio][" + bindingAttr + "=" + group_name + "]:checked";
|
379
|
-
var value = view.$(value_selector).val();
|
380
|
-
setModelValue(group_name, value);
|
381
|
-
}
|
382
|
-
}
|
383
|
-
});
|
384
|
-
};
|
385
|
-
|
386
|
-
return methods;
|
387
|
-
})(Backbone);
|
388
|
-
|
389
|
-
// ----------------------------
|
390
|
-
// Checkbox Bi-Directional Binding Methods
|
391
|
-
// ----------------------------
|
392
|
-
var CheckboxBinding = (function(Backbone){
|
393
|
-
var methods = {};
|
394
|
-
|
395
|
-
methods.bind = function(selector, view, model, config){
|
396
|
-
var modelBinder = this;
|
397
|
-
|
398
|
-
view.$(selector).each(function(index){
|
399
|
-
var element = view.$(this);
|
400
|
-
var bindingAttr = config.getBindingAttr('checkbox');
|
401
|
-
var attribute_name = config.getBindingValue(element, 'checkbox');
|
402
|
-
|
403
|
-
if (!attribute_name) return;
|
404
|
-
|
405
|
-
var modelChange = function(model, val){
|
406
|
-
if (val){
|
407
|
-
element.attr("checked", "checked");
|
408
|
-
}
|
409
|
-
else{
|
410
|
-
element.removeAttr("checked");
|
411
|
-
}
|
412
|
-
};
|
413
|
-
|
414
|
-
var setModelValue = function(attr_name, value){
|
415
|
-
var data = {};
|
416
|
-
data[attr_name] = value;
|
417
|
-
model.set(data);
|
418
|
-
};
|
419
|
-
|
420
|
-
var elementChange = function(ev){
|
421
|
-
var changedElement = view.$(ev.target);
|
422
|
-
var checked = changedElement.is(":checked")? true : false;
|
423
|
-
setModelValue(attribute_name, checked);
|
424
|
-
};
|
425
|
-
|
426
|
-
modelBinder.registerModelBinding(model, attribute_name, modelChange);
|
427
|
-
modelBinder.registerElementBinding(element, elementChange);
|
428
|
-
|
429
|
-
var attr_exists = model.attributes.hasOwnProperty(attribute_name);
|
430
|
-
if (attr_exists) {
|
431
|
-
// set the default value on the form, from the model
|
432
|
-
var attr_value = getAttributeValue(model, attribute_name);
|
433
|
-
if (typeof attr_value !== "undefined" && attr_value !== null && attr_value != false) {
|
434
|
-
element.attr("checked", "checked");
|
435
|
-
}
|
436
|
-
else{
|
437
|
-
element.removeAttr("checked");
|
438
|
-
}
|
439
|
-
} else {
|
440
|
-
// bind the form's value to the model
|
441
|
-
var checked = element.is(":checked")? true : false;
|
442
|
-
setModelValue(attribute_name, checked);
|
443
|
-
}
|
444
|
-
});
|
445
|
-
};
|
446
|
-
|
447
|
-
return methods;
|
448
|
-
})(Backbone);
|
449
|
-
|
450
|
-
// ----------------------------
|
451
|
-
// Data-Bind Binding Methods
|
452
|
-
// ----------------------------
|
453
|
-
var DataBindBinding = (function(Backbone, _, $){
|
454
|
-
var dataBindSubstConfig = {
|
455
|
-
"default": ""
|
456
|
-
};
|
457
|
-
|
458
|
-
modelBinding.Configuration.dataBindSubst = function(config){
|
459
|
-
this.storeDataBindSubstConfig();
|
460
|
-
_.extend(dataBindSubstConfig, config);
|
461
|
-
};
|
462
|
-
|
463
|
-
modelBinding.Configuration.storeDataBindSubstConfig = function(){
|
464
|
-
modelBinding.Configuration._dataBindSubstConfig = _.clone(dataBindSubstConfig);
|
465
|
-
};
|
466
|
-
|
467
|
-
modelBinding.Configuration.restoreDataBindSubstConfig = function(){
|
468
|
-
if (modelBinding.Configuration._dataBindSubstConfig){
|
469
|
-
dataBindSubstConfig = modelBinding.Configuration._dataBindSubstConfig;
|
470
|
-
delete modelBinding.Configuration._dataBindSubstConfig;
|
471
|
-
}
|
472
|
-
};
|
473
|
-
|
474
|
-
modelBinding.Configuration.getDataBindSubst = function(elementType, value){
|
475
|
-
var returnValue = value;
|
476
|
-
if (value === undefined){
|
477
|
-
if (dataBindSubstConfig.hasOwnProperty(elementType)){
|
478
|
-
returnValue = dataBindSubstConfig[elementType];
|
479
|
-
} else {
|
480
|
-
returnValue = dataBindSubstConfig["default"];
|
481
|
-
}
|
482
|
-
}
|
483
|
-
return returnValue;
|
484
|
-
};
|
485
|
-
|
486
|
-
var setOnElement = function(element, attr, val){
|
487
|
-
var valBefore = val;
|
488
|
-
val = modelBinding.Configuration.getDataBindSubst(attr, val);
|
489
|
-
val = modelBinding.getMediator(element).render(val);
|
490
|
-
switch(attr){
|
491
|
-
case "html":
|
492
|
-
element.html(val);
|
493
|
-
break;
|
494
|
-
case "text":
|
495
|
-
element.text(val);
|
496
|
-
break;
|
497
|
-
case "enabled":
|
498
|
-
element.attr("disabled", !val);
|
499
|
-
break;
|
500
|
-
case "displayed":
|
501
|
-
element[val? "show" : "hide"]();
|
502
|
-
break;
|
503
|
-
case "hidden":
|
504
|
-
element[val? "hide" : "show"]();
|
505
|
-
break;
|
506
|
-
default:
|
507
|
-
element.attr(attr, val);
|
508
|
-
}
|
509
|
-
};
|
510
|
-
|
511
|
-
var splitBindingAttr = function(element)
|
512
|
-
{
|
513
|
-
var dataBindConfigList = [];
|
514
|
-
var dataBindAttributeName = modelBinding.Conventions.databind.selector.replace(/^(.*\[)([^\]]*)(].*)/g, '$2');
|
515
|
-
var databindList = element.attr(dataBindAttributeName).split(";");
|
516
|
-
_.each(databindList, function(attrbind){
|
517
|
-
var databind = $.trim(attrbind).split(" ");
|
518
|
-
|
519
|
-
// make the default special case "text" if none specified
|
520
|
-
if( databind.length == 1 ) databind.unshift("text");
|
521
|
-
|
522
|
-
dataBindConfigList.push({
|
523
|
-
elementAttr: databind[0],
|
524
|
-
modelAttr: databind[1]
|
525
|
-
});
|
526
|
-
});
|
527
|
-
return dataBindConfigList;
|
528
|
-
};
|
529
|
-
|
530
|
-
var getEventConfiguration = function(element, databind){
|
531
|
-
var config = {};
|
532
|
-
var eventName = databind.modelAttr;
|
533
|
-
var index = eventName.indexOf("event:");
|
534
|
-
|
535
|
-
if (index == 0) {
|
536
|
-
// "event:foo" binding
|
537
|
-
config.name = eventName.substr(6);
|
538
|
-
config.callback = function(val){
|
539
|
-
setOnElement(element, databind.elementAttr, val);
|
540
|
-
};
|
541
|
-
} else {
|
542
|
-
// standard model attribute binding
|
543
|
-
config.name = "change:" + eventName;
|
544
|
-
config.callback = function(model, val){
|
545
|
-
setOnElement(element, databind.elementAttr, val);
|
546
|
-
};
|
547
|
-
}
|
548
|
-
|
549
|
-
return config;
|
550
|
-
}
|
551
|
-
var methods = {};
|
552
|
-
|
553
|
-
methods.bind = function(selector, view, model, config){
|
554
|
-
var modelBinder = this;
|
555
|
-
|
556
|
-
view.$(selector).each(function(index){
|
557
|
-
var element = view.$(this);
|
558
|
-
var databindList = splitBindingAttr(element);
|
559
|
-
|
560
|
-
_.each(databindList, function(databind){
|
561
|
-
var eventConfig = getEventConfiguration(element, databind);
|
562
|
-
modelBinder.registerDataBinding(model, eventConfig.name, eventConfig.callback);
|
563
|
-
|
564
|
-
// set default on data-bind element
|
565
|
-
setOnElement(element, databind.elementAttr, getAttributeValue(model, databind.modelAttr));
|
566
|
-
});
|
567
|
-
|
568
|
-
});
|
569
|
-
};
|
570
|
-
|
571
|
-
return methods;
|
572
|
-
})(Backbone, _, $);
|
573
|
-
|
574
|
-
|
575
|
-
// ----------------------------
|
576
|
-
// Binding Conventions
|
577
|
-
// ----------------------------
|
578
|
-
modelBinding.Conventions = {
|
579
|
-
text: {selector: "input:text", handler: StandardBinding},
|
580
|
-
textarea: {selector: "textarea", handler: StandardBinding},
|
581
|
-
password: {selector: "input:password", handler: StandardBinding},
|
582
|
-
radio: {selector: "input:radio", handler: RadioGroupBinding},
|
583
|
-
checkbox: {selector: "input:checkbox", handler: CheckboxBinding},
|
584
|
-
select: {selector: "select", handler: SelectBoxBinding},
|
585
|
-
databind: { selector: "*[data-bind]", handler: DataBindBinding},
|
586
|
-
// HTML5 input
|
587
|
-
number: {selector: "input[type=number]", handler: StandardBinding},
|
588
|
-
range: {selector: "input[type=range]", handler: StandardBinding},
|
589
|
-
tel: {selector: "input[type=tel]", handler: StandardBinding},
|
590
|
-
search: {selector: "input[type=search]", handler: StandardBinding},
|
591
|
-
url: {selector: "input[type=url]", handler: StandardBinding},
|
592
|
-
email: {selector: "input[type=email]", handler: StandardBinding}
|
593
|
-
};
|
594
|
-
|
595
|
-
modelBinding.Mediators = {}
|
596
|
-
|
597
|
-
modelBinding.getMediator = function(element) {
|
598
|
-
var mediator = modelBinding.Mediators[$(element).attr("data-mediator")] || {};
|
599
|
-
|
600
|
-
mediator.parse = mediator.parse || function(t) { return t; }
|
601
|
-
mediator.render = mediator.render || function(t) { return t; }
|
602
|
-
return mediator;
|
603
|
-
}
|
604
|
-
|
605
|
-
return modelBinding;
|
606
|
-
});
|
607
|
-
|
608
|
-
// Backbone.Modelbinding AMD wrapper with namespace fallback
|
609
|
-
if (typeof define === 'function' && define.amd) {
|
610
|
-
// AMD support
|
611
|
-
define([
|
612
|
-
'backbone', // use Backbone 0.5.3-optamd3 branch (https://github.com/jrburke/backbone/tree/optamd3)
|
613
|
-
'underscore', // AMD supported
|
614
|
-
'jquery' // AMD supported
|
615
|
-
], function (Backbone, _, jQuery) {
|
616
|
-
return modelbinding(Backbone, _, jQuery);
|
617
|
-
});
|
618
|
-
} else {
|
619
|
-
// No AMD, use Backbone namespace
|
620
|
-
root.Backbone = Backbone || {};
|
621
|
-
root.Backbone.ModelBinding = modelbinding(Backbone, _, jQuery);
|
622
|
-
}
|
623
|
-
|
624
|
-
})(this);
|