-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexko.html
167 lines (137 loc) · 4.81 KB
/
indexko.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
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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script src="js/knockout.2.0.0.js"></script>
<script src="js/sammy.js"></script>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<link rel="stylesheet" type="text/css" href="css/nav.css"/>
</head>
<body>
<hr>
<h2>debug</h2>
MenuItem: <span data-bind="text:selectedMenuItem"></span>
<br>
MenuData: <span data-bind="text:ko.toJSON(selectedMenuData)"></span>
<br>
template: <span data-bind="text:templateToUse()"></span>
<br>
columns: <span data-bind="text:ko.toJSON(templateColumns)"></span>
<hr>
<!-- Menu -->
<ul class="menu" data-bind="foreach: menuItems">
<li data-bind="text: $data,
css: { selected: $data == $root.selectedMenuItem() },
click: $root.goToScreen"></li>
</ul>
<!-- function here to lookup data for boilerplate messaging: getScreenData(...) = {header:'blah', footer:'blah'}-->
<table>
<thead><tr data-bind="template:{name:'tableHeader', foreach:templateColumns}"></tr></thead>
<tbody data-bind="template:{name:'tableBody', foreach:selectedMenuData}"</tbody>
</table>
<script id="tableHeader" type="text/html">
<td><span data-bind="text:$data"></span></td>
</script>
<script id="tableBody" type="text/html">
<tr data-bind="foreach: $parent.templateColumns"><td><span data-bind="text:$parent[$data]"></span></td></tr>
</script>
<script type="text/javascript">
//var MenuItem = function(name) {
// this.name = ko.observable(name);
//http://jsfiddle.net/rniemeyer/R5MtC/
//this.isSelected = ko.computed(function() {
//return this === parent.selectedItem();
// }, this);
// this.dataColumns = ko.computed(function() {
//
// }, this);
//};
function FBViewModel () {
//Data
var self = this;
self.menuItems = ['Home','Teams','Players','Conferences'];
self.selectedMenuItem = ko.observable("Home");
self.selectedMenuData = ko.observableArray();
self.templateColumns = ko.computed(function() {
//get the first item in the array, build a list of its object properties to serve as columns //TODO: a way to cache this, maybe an array of [menuItem (key), cachedData (value)]
if ((self.selectedMenuData() != null) && (self.selectedMenuData()[0] != undefined))
{
var obj = self.selectedMenuData()[0];
var props = [];
for (var prop in obj) {
props.push(prop);
}
return props;
}
return [];
}, self);
//template switching
self.templateToUse = function() {return self.selectedMenuItem()}.bind(FBViewModel);
//Behaviors
self.goToScreen = function (menuItem) { location.hash = menuItem; };
// Client-side routes
Sammy(function (context) {
this.get('#:menuItem', function () {
var selectedItem = this.params.menuItem;
self.selectedMenuData(null);
var a = self.templateColumns;
if (selectedItem === 'Home') //more for things that aren't loading data?
{
self.selectedMenuItem(selectedItem);
self.selectedMenuData([{"info": "Home Data here"}]);
return false;
}
//only do this for things i know how to handle
//if (!(self.menuItems.contains(this.params.menuItem)))
//{
// return false;
//}
//get file name
var dataFile = selectedItem.toLowerCase() + '.json';
console.log(dataFile);
self.firstName = dataFile;
console.log('first: ' + self.firstName);
//todo - cache these
$.ajax({
url: 'data/' + dataFile,
dataType: 'json',
success: function(items) {
//alert ("updating menu item to " + selectedItem);
self.selectedMenuItem(selectedItem);
//alert ("updating menu data") ;
//set the view & data //todo - switch view when caching. error handle.
self.selectedMenuData(items);
}
});
//$.get("/mail", { folder: this.params.folder }, self.selectedFolderData);
});
this.get('#:menuItem/:itemId', function () {
self.selectedMenuItem(this.params.menuItem);
self.selectedMenuData(null);
//$.get("/mail", { mailId: this.params.mailId }, self.selectedMailData);
});
//home screen
//this.get('', function () { this.app.runRoute('get', '#Inbox') });
}).run();
}
var model = new FBViewModel;
ko.applyBindings(model);
//string.format equiv
String.format = String.prototype.format = function() {
var i=0;
var string = (typeof(this) == "function" && !(i++)) ? arguments[0] : this;
for (; i < arguments.length; i++)
string = string.replace(/\{\d+?\}/, arguments[i]);
return string;
}
Array.prototype.contains = function (itemId){
for (index in this) { //todo check speed
if (this[index].name === itemId)
{
return true;
}
}
return false;
};
</script>
</body>
</html>