-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathdemo-convert.html
More file actions
77 lines (72 loc) · 2.05 KB
/
demo-convert.html
File metadata and controls
77 lines (72 loc) · 2.05 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Model Convert 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 Convert Demo</h1>
<p>This demo shows converting date strings sent by the
server to JavaScript dates with attributes and convert.</p>
</div>
<div id="demo-html">
<ul id='contacts'></ul>
</div>
<script type='text/javascript'
src='../../steal/steal.js'>
</script>
<script type='text/javascript' id="demo-source">
steal('jquery/model',
'jquery/dom/fixture',
function(){
$.fixture("/foobar.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'}]];
});
$.Model("Contact",{
attributes : {
// labels birthday a date
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 : '/foobar.json'
},{
// helper function that returns age in years
age : function(){
return new Date().getFullYear() -
this.birthday.getFullYear()
}
});
// get all contacts and put them in the page
Contact.findAll( {}, function( contacts ){
var html = [];
for(var i =0; i < contacts.length; i++){
html.push('<li>'+contacts[i].age() + '</li>')
}
$('#contacts').html( html.join('') );
});
})
</script>
</body>
</html>