-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (114 loc) · 2.83 KB
/
app.js
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
//
// SETUP and CONFIGURATION
//
function setup(options) {
initializeApp(options);
defineDatabase(options, function() {
Fruit = defineModel({
table: 'fruits',
fields: {
name: "TEXT",
color: "TEXT"
}
});
});
}
//
// EVENTS
//
$('#home_page').live('pageinit', function() {
// app initialization can go here.
});
//
// FRUIT LIST
//
$('.list_fruit_button').live('click', function() {
Fruit.findAll({order:"name"}, function(rows) {
showFruitList(rows);
});
});
//
// FRUIT LIST CLICK
//
$('.fruit_list a').live('click', function() {
// get the data-id of the link that was clicked
var id = $(this).attr('data-id');
// find the record and load its details page
Fruit.findByID(id, function(record) {
showFruit(record);
});
});
//
// COLOR LIST
//
$('.list_colors_button').live('click', function() {
var sql = 'SELECT *, count(*) as count FROM fruits GROUP BY color ORDER BY color';
Fruit.findBySql(sql, function(rows) {
showColorsList(rows);
});
});
//
// COLOR LIST CLICK
//
$('.color_list a').live('click', function() {
// get the data-id of the link that was clicked
var color = $(this).attr('data-id');
// find the record and load its details page
Fruit.findAll({where:["color","=",color]}, function(rows) {
showFruitList(rows, color + " Fruit");
});
});
//
// VIEWS
//
//
// shows a list of fruit records.
//
// arguments:
// records -- a list of fruit records (required).
//
function showFruitList(records, title) {
// update the list with records
var ul = $(".fruit_list");
ul.empty(); // clear list
records.forEach(function(record) {
var li = buildListItem({id:record.id, label:record.name});
ul.append(li);
});
if (title) {
$('.fruit_list_title').html(title);
} else {
$('.fruit_list_title').html("Fruit");
}
// jump to the page
$.mobile.changePage('#list_fruit_page');
// refresh the list view
ul.listview('refresh');
}
//
// shows the list of colors
//
function showColorsList(records) {
var ul = $(".color_list");
ul.empty(); // clear list
records.forEach(function(record) {
var li = buildListItem({id:record.color, label:record.color, count:record.count});
ul.append(li);
});
$.mobile.changePage('#list_colors_page');
ul.listview('refresh');
}
//
// shows details for a particular fruit.
//
// arguments:
// fruit -- a single fruit record (required).
//
function showFruit(fruit) {
// change the text on the show_fruit page
$('.fruit_title').html("Fruit: " + fruit.name);
$('.fruit_name').html(fruit.name);
$('.fruit_color').html(fruit.color);
// switch to the show_fruit page.
$.mobile.changePage('#show_fruit_page');
}