-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·123 lines (100 loc) · 3.47 KB
/
app.js
File metadata and controls
executable file
·123 lines (100 loc) · 3.47 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
require(['storehouse/Storehouse', 'dojo/on', 'dojo/html'], function (Storehouse, on, html) {
var tpls = {
row: '<tr><td>{customerid}</td><td><input id="lastname_{customerid}" value="{lastname}"></td><td><input id="firstname_{customerid}" value="{firstname}"></td><td><button onclick="app.deleteItem(\'{customerid}\');">delete</button><button onclick="app.updateItem(\'{customerid}\');">update</button></td></tr>',
table: '<table><tbody><tr><th>ID</th><th>Last Name</th><th>First Name</th><th></th></tr>{content}</table></tbody>'
};
var customers = new Storehouse({
storeId: 'customer',
idProperty: 'customerid'
});
var nodeCache = {};
function init () {
// open the store
customers.open().then(refreshTable);
// create references for some nodes we have to work with
dojo.forEach(['customerid', 'firstname', 'lastname', 'submit', 'results-container'], function (id) {
nodeCache[id] = document.getElementById(id);
});
// and listen to the form's submit button.
on(nodeCache.submit, 'click', enterData);
}
function refreshTable () {
listItems(customers.data);
}
function listItems (data) {
var content = '';
dojo.forEach(data, function (item) {
content += tpls.row.replace(/\{([^\}]+)\}/g, function (_, key) {
return item[key];
});
});
var node = dojo.byId('results-container');
html.set(node, tpls.table.replace('{content}', content));
}
function enterData () {
// read data from inputs…
var data = {};
dojo.forEach(['customerid', 'firstname', 'lastname'], function (key) {
var value = dojo.trim(nodeCache[key].value);
if (value.length) {
if (key == 'customerid') {
value = checkForNumericId(value);
}
data[key] = value;
}
});
// …and store them away.
customers.put(data).then(function () {
clearForm();
refreshTable();
});
}
function clearForm () {
dojo.forEach(['customerid', 'firstname', 'lastname'], function (id) {
nodeCache[id].value = '';
});
}
function checkForNumericId (id) {
var numericId = parseInt(id, 10);
return isNaN(numericId) ? id : numericId;
}
function deleteItem (id) {
id = checkForNumericId(id);
customers.remove(id).then(refreshTable);
}
function updateItem (id) {
id = checkForNumericId(id);
var data = {
customerid: id,
firstname: dojo.trim(document.getElementById('firstname_' + id).value),
lastname: dojo.trim(document.getElementById('lastname_' + id).value)
};
customers.put(data).then(refreshTable);
}
function makeRandomEntry () {
var lastnames = ['Smith', 'Miller', 'Doe', 'Frankenstein', 'Furter'],
firstnames = ['Peter', 'John', 'Frank', 'James', 'Jill'];
var entry = {
lastname: lastnames[Math.floor(Math.random() * 5)],
firstname: firstnames[Math.floor(Math.random() * 4)],
age: Math.floor(Math.random() * (100 - 20)) + 20
};
return entry;
}
function addRandomCustomer () {
var data = makeRandomEntry();
customers.put(data).then(function () {
clearForm();
refreshTable();
});
}
// export some functions to the outside to
// make the onclick="" attributes work.
window.app = {
deleteItem: deleteItem,
updateItem: updateItem,
addRandomCustomer: addRandomCustomer
};
// go!
init();
});