-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-backbone3.html
84 lines (80 loc) · 2.81 KB
/
test-backbone3.html
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
<html>
<head>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript" src="lib/backbone.js"></script>
<script type="text/javascript" src="Backbone.ModelBinder.js"></script>
<script type="text/javascript" src="Backbone.CollectionBinder.js"></script>
<script>
(function extendBackboneBinder () {
Backbone.ModelBinder.prototype._isElUserEditable = function (el) {
return true;
}
Backbone.ModelBinder.prototype._getElValue = function(elementBinding, el){
var elType = el.attr('type');
if(elType == null)
return el.html();
switch (elType) {
case 'checkbox':
return el.prop('checked') ? true : false;
default:
if(el.attr('contenteditable') !== undefined){
return el.html();
}
else {
return el.val();
}
}
};
Backbone.CollectionBinder.prototype.copyViewValuesToCollection = function(){
var parentEl = $(this._elManagerFactory._getParentEl());
var models = [];
var children = parentEl.children();
for (var ii = 0; ii < children.length; ii++) {
var child = children[ii];
var model = new Backbone.Model();
var binder = new Backbone.ModelBinder();
binder.bind(model, child, this._options);
binder.copyViewValuesToModel();
models.push(model);
}
this._collection.add(models, {silent: true});
};
})();
function startCopying() {
if(!window.collectionResult) {window.collectionResult = new Backbone.Collection([]);}
var elManagerFactory = new Backbone.CollectionBinder.ElManagerFactory('something wont be used');
collectionBinder = new Backbone.CollectionBinder(elManagerFactory, {id:'>:nth-child(1)', name:'>:nth-child(2)'});
Backbone.ModelBinder.SetOptions({initialCopyDirection: Backbone.ModelBinder.Constants.ViewToModel});
collectionBinder.bind(window.collectionResult, jQuery('#example-1-result tbody'));
collectionBinder.copyViewValuesToCollection();
console.log(JSON.stringify(window.collectionResult));
}
</script>
</head>
<body>
<a href="javascript:startCopying()">Start Test!</a>
<table id="example-1-result">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>12</td>
<td>Josh</td>
</tr>
<tr>
<td>13</td>
<td>John</td>
</tr>
<tr>
<td>14</td>
<td>Jack</td>
</tr>
</tbody>
</table>
</body>
</html>