-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathdemo-events.html
More file actions
187 lines (159 loc) · 4.19 KB
/
demo-events.html
File metadata and controls
187 lines (159 loc) · 4.19 KB
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Model Events Demo</title>
<style type='text/css'>
body {font-family: verdana}
li {border: solid 1px gray; padding: 5px; width: 250px;}
li a {color: red; font-weight: bold;}
p {width: 400px;}
</style>
</head>
<body>
<div id="demo-instructions">
<h1>Model Events Demo</h1>
<p>This demo shows listening to model update events.
Clicking on a person's name will show a form to update
their birthday. Change the birthday and 'blur' the
input to update their listed age.</p>
</div>
<div id="demo-html">
<div id='update'></div>
<h2>Direct Binding</h2>
<p>
The following list binds
directly to "birthday" events.
</p>
<div id='contacts1'></div>
<h2>Model Binding</h2>
<p>
The following list binds
to "updated" events on Contact.
</p>
<div id='contacts2'></div>
</div>
<script type='text/javascript'
src='../../steal/steal.js'>
</script>
<script type='text/javascript'>
steal('jquery/model',
'jquery/dom/fixture',
'jquery/model/list',
'jquery/controller',
function(){
// =============== Setup Fixtures ===============
$.fixture("GET /contacts.json", function(){
return [[{'id': 1,'name' : 'Justin Meyer','birthday': '1982-10-20'},
{'id': 2,'name' : 'Brian Moschel','birthday': '1983-11-10'},
{'id': 3,'name' : 'Alex Gomes','birthday': '1980-2-10'}]];
});
$.fixture("PUT /contacts/{id}.json", function(){
return {};
})
// =============== Contacts Model ===============
$.Model("Contact",{
attributes : {
birthday : 'date'
},
convert : {
// a date converter helper
date : function(raw){
if(typeof raw == 'string'){
// if a string, convert to a date
var matches = raw.match(/(\d+)-(\d+)-(\d+)/)
return new Date( matches[1],
(+matches[2])-1,
matches[3] )
}else if(raw instanceof Date){
return raw;
}
}
},
findAll : "/contacts.json",
update : "/contacts/{id}.json"
},{
ageThisYear : function(){
return new Date().getFullYear() -
this.birthday.getFullYear()
},
getBirthday : function(){
return ""+this.birthday.getFullYear()+
"-"+(this.birthday.getMonth()+1)+
"-"+this.birthday.getDate();
}
});
// updates the name of a contact
$.Controller("NameUpdater",{
init : function(){
this.draw();
},
update : function(options){
this._super(options);
this.draw();
},
draw : function(){
var contact = this.options.contact;
this.element.html("")
.append(contact.name+"'s birthday")
.append( $('<input/>').val(contact.attr("birthday")) )
},
"input change" : function(el){
this.options.contact.update({
'birthday': el.val()
})
}
});
// =============== A list that binds on each contact ===============
bindOnEachContact = function(contacts){
var contactsEl = $('#contacts1');
$.each(contacts, function(i, contact){
var li = $('<li>')
.model(contact)
.html(contact.name+" "+contact.ageThisYear()+
" <a>Show</a>")
.appendTo(contactsEl);
contact.bind("birthday", function(){
li.html(contact.name+" "+this.ageThisYear()+
" <a>Show</a>");
})
})
};
$('#contacts1').delegate("li","click", function(){
$('#update').name_updater({
contact : $(this).closest('.contact').model()
});
});
makeListWithoutBinding = function(contacts){
var contactsEl = $('#contacts2'),
html = [],
contact;
for(var i =0; i < contacts.length;i++){
contact = contacts[i]
html.push("<li class='contact ",
contact.identity(),"'>",
contact.name+" "+contact.ageThisYear()+
" <a>Show</a>",
"</li>")
}
contactsEl.html(html.join(""))
$('#contacts2').delegate("li","click", function(){
$('#update').name_updater({
contact : contacts.get(this)[0]
});
});
};
Contact.bind("updated", function(ev, contact){
contact.elements($('#contacts2'))
.html(contact.name+" "+contact.ageThisYear()+
" <a>Show</a>");
})
// List 1
Contact.findAll({},function(contacts){
bindOnEachContact(contacts);
makeListWithoutBinding(contacts)
});
});
</script>
</body>
</html>