-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
73 lines (61 loc) · 1.95 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>KnockoutJS test page</title>
<script src="jquery-1.6.1.min.js"></script>
<script src="jquery.tmpl.min.js"></script>
<script src="knockout-1.2.1.js"></script>
<script src="knockout.mapping-1.0.js"></script>
</head>
<body>
<input data-bind="value: firstname" />
<input data-bind="value: lastname" />
<div data-bind='template: "itemTemplate"'> </div>
<script id='itemTemplate' type='text/html'>
${ fullname } is ${ age } years old
<button data-bind='click: makeOlder'>Make older</button>
</script>
<script id='feedTemplate' type='text/html'>
<p>${ name } (${ id }) : ${ phone }</p>
</script>
<span data-bind="text: formatedTime"></span>
<div data-bind='template: { name: "feedTemplate", foreach: viewModel.feed }'>
</div>
<span data-bind="text: feedValue"></span>
<script>
function displayTime(seconds) {
return Math.floor( seconds / 60) + ":" + ((seconds % 60).toFixed(0) < 10 ? '0' : '') + (seconds % 60).toFixed(0)
}
var viewModel = {
firstname: ko.observable("Kent"),
lastname: ko.observable("Jordan"),
age: ko.observable(18),
refresh: ko.observable(10),
description: ko.observable("Test"),
feed: ko.observableArray([]),
makeOlder: function() {
this.age(this.age() + 1);
}
};
viewModel.fullname = ko.dependentObservable(function() {
return this.firstname()+" "+this.lastname();
}, viewModel);
viewModel.formatedTime = ko.dependentObservable(function() {
return displayTime(this.refresh());
}, viewModel);
viewModel.feedValue = ko.dependentObservable(function() {
return this.feed().length;
}, viewModel);
ko.applyBindings(viewModel);
setInterval(function() {
viewModel.refresh(viewModel.refresh() - 1);
if(viewModel.refresh()==-1) {
jQuery.get('sample_data.json', function(resp) {
viewModel.feed(jQuery.parseJSON(resp));
});
viewModel.refresh(10);
}
}, 1000);
</script>
</body>
</html>