-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathExample_NestedAttributes.html
87 lines (67 loc) · 3.1 KB
/
Example_NestedAttributes.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
85
86
87
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type"/>
<title>Example - Nested Attributes</title>
<!-- include source files here... -->
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../lib/jquery.js"></script>
<script type="text/javascript" src="../lib/backbone.js"></script>
<script type="text/javascript" src="deep-model.js"></script>
<script type="text/javascript" src="../Backbone.ModelBinder.js"></script>
<script>
$().ready(function () {
model = new Backbone.DeepModel({firstName:'Bob', home:{squareFeet:'2000', garage:{carCapacity:'2'}}});
model.bind('change', function () {
$('#modelData').html(JSON.stringify(model.toJSON()));
});
model.trigger('change'); // just to show the #modelData values initially, not needed for the ModelBinder
ViewClass = Backbone.View.extend({
_modelBinder:undefined,
initialize:function () {
this._modelBinder = new Backbone.ModelBinder();
},
render:function () {
var html = '\
Edit your information: <span name="firstName"></span> <br><br>\
<span class="label"> \
firstName: </span> <input type="text" name="firstName"/><br>\
home.squareFeet: <input type="text" name="home.squareFeet"/><br>\
home.garage.carCapacity: <input type="text" name="home.garage.carCapacity"/><br>\
';
this.$el.html(html);
var bindings = {
'firstName' : '[name=firstName]',
'home.squareFeet': {selector: '[name="home.squareFeet"]', converter: function(direction, value){
if(direction === Backbone.ModelBinder.Constants.ModelToView){
return value + ' sq feet';
}
else {
return value.replace(' sq feet', '');
}
}},
'home.garage.carCapacity': '[name="home.garage.carCapacity"]'
};
this._modelBinder.bind(this.model, this.el, bindings);
return this;
}
});
view = new ViewClass({model:model});
$('#viewContent').append(view.render().el);
});
</script>
</head>
<body>
<br>
Example to discuss : <a href="https://github.com/theironcook/Backbone.ModelBinder/issues/3"> ModelBinder Issue 3 </a>
<br>
How to bind to nested attributes that are not Backbone Models.
<br>
<br>
Model data:
<div id="modelData"></div>
<hr>
<br>
<div id="viewContent"></div>
</body>
</html>