forked from UKHomeOffice/esapi-policeuk-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
286 lines (258 loc) · 10.4 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* --------------------------------------
Below are several examples of hooking
up a Falcor model and getting data from
it to be displayed on the screen.
Step 1: A local model cache is setup
with some data. Data is requested with
various get requests to the model.
Step 2: We change the model over to a
JSON Graph with references so that data
isn't duplicated.
Step 3: We move the data over to the
server to be served from a Falcor Router.
This step is currently setup to run but
you can comment it out and start from
Step 1 by uncommenting the appropriate
pieces.
----------------------------------------*/
var $ref = falcor.Model.ref;
/* === Step 1 === */
// We can prime the model cache with a new falcor.Model
var localmodel = new falcor.Model({
cache: {
forcesById: {
"avon-and-somerset": {
name: "Avon and Somerset Constabulary",
description: {$type: "ref", value: ["dbi", "avon-and-somerset"]}
},
"city-of-london": {
name: "City of London Police",
description: $ref("dbi['city-of-london']")
}
},
dbi: {
"avon-and-somerset": "Yo yo",
"city-of-london": "City Police",
"city-of-madup": "mdup"
},
forces: [
$ref("forcesById['city-of-london']") ,
$ref("forcesById['avon-and-somerset']")
]
}
});
/* === Step 2 === */
// We can use the shorthand for references with a variable
// var model = new falcor.Model({
// cache: {
// locationsById: {
// 1: {
// city: "Salt Lake City",
// state: "Utah"
// },
// 2: {
// city: "Las Vegas",
// state: "Nevada"
// },
// 3: {
// city: "Minneapolis",
// state: "Minnesota"
// },
// 4: {
// city: "Walker Creek Ranch",
// state: "California"
// }
// },
// events: [
// {
// name: "ng-conf",
// description: "The world's best Angular Conference",
// location: $ref('locationsById[1]')
// },
// {
// name: "React Rally",
// description: "Conference focusing on Facebook's React",
// location: $ref('locationsById[1]')
// },
// {
// name: "ng-Vegas",
// description: "Two days jam-packed with Angular goodness with a focus on Angular 2",
// location: $ref('locationsById[2]')
// },
// {
// name: "Midwest JS",
// description: "Midwest JS is a premier technology conference focused on the JavaScript ecosystem.",
// location: $ref('locationsById[3]')
// },
// {
// name: "NodeConf",
// description: "NodeConf is the longest running community driven conference for the Node community.",
// location: $ref('locationsById[4]')
// }
// ]
// }
// });
// model
// // Now when we set Utah to UT for the first occurrence, it is changed everywhere else
// .set(falcor.pathValue(["events", 0, "location", "state"], 'UT'))
// .then(function(response) {
// model
// .get(["events", {from: 0, to: 2}, ["name", "description", "location"],["city", "state"]])
// .then(function(response) {
// document.getElementById('event-data').innerHTML = JSON.stringify(response, null, 2);
// });
// });
/* === Step 3 === */
// We can set the model to have a data source that is retrieved from the backend
// over HTTP by setting the soure to be a falcor.HttpDataSource.
var model = new falcor.Model({source: new falcor.HttpDataSource('/model.json')});
// model
// // .get(["events", {from: 0, to: 2}, ["name", "description"]])
// .get([
// "events", {from: 0, to: 2}, ["name", "description"]
// ],
// [
// 'events', {from: 0, to: 2}, 'location', ['city', 'state']
// ])
// model
// .get(["events", {from: 0, to: 2}, ["name", "description", "location"],["city", "state"]])
// .then(function(response) {
// document.getElementById('event-data').innerHTML = JSON.stringify(response, null, 2);
// });
// Search example - we pass "Midwest JS" which will be looked up
// in the events data on the server and sent back if it exists
model
.get(["events", "byName", ["Midwest JS"], ['description']])
.then(function(response) {
document.getElementById('event-data').innerHTML = JSON.stringify(response, null, 2);
}, function(err) {
console.log(err);
// console.log(err['0'].value.message);
});
// model
// .get(["forces", {from: 0, to: 3}, ["id", "name"]])
// .then(function(response) {
// document.getElementById('forces-data').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// // console.log(err['0'].value.message);
// });
// model
// .get(["forcesById", ["avon-and-somerset", "city-of-london", "metropolitan"], ["name", "description"]])
// .then(function(response) {
// document.getElementById('forces-data2').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data2').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
//// peopleByForceIdAndNeighbourhoodCode
// model
// .get(
// ["peopleByForceIdAndNeighbourhoodCode", ["leicestershire"], ["NC04"], [0, 1, 2], ["bio", "name", "rank"]])
// //,"latitude", "longitude", "type", "population"]])
// .then(function(response) {
// console.log('got response', response);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log('error', err);
// document.getElementById('forces-data3').innerHTML = "<h3>Error</h3>" + JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
model
.get(
["forces", {from: 0, to: 20}, ["name", "description", "neighbourhoods"], {from: 0, to: 10}, ["id", "name"]]
)
.then(function(response) {
console.log('got response', response);
document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
}, function(err) {
console.log('error', err);
document.getElementById('forces-data3').innerHTML = "<h3>Error</h3>" + JSON.stringify(err, null, 2);
// console.log(err['0'].value.message);
});
//// getting to people via force.neighbourhood
// model
// .get(
// ["forces", {from: 0, to: 5}, ["id","name","neighbourhoods"], {from: 0, to: 2},["people"],{from: 0, to: 1}, ["bio", "name", "rank"]],
// ["forces", {from: 8, to: 10}, ["id","name", "neighbourhoods"], {from: 0, to: 2},["people"],{from: 0, to: 1}, ["bio", "name", "rank"]])
// //,"latitude", "longitude", "type", "population"]])
// .then(function(response) {
// console.log('got response', response);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log('error', err);
// document.getElementById('forces-data3').innerHTML = "<h3>Error</h3>" + JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// model
// .get(
// [["forcesById"], ["leicestershire"] , ["neighbourhoods"], "NC04", ["people"], ["bio"]],
// ["forces",{from: 5, to: 10}, ["neighbourhoods", "description", "id"], {from: 0, to: 1}, ["location"], [0],
// ["address","postcode"]])
// //,"latitude", "longitude", "type", "population"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// stations by forceidx and neighbourhood, kinda works
// model
// .get(
// ["force",
// [2, 3, 7], ["neighbourhoods", "description", "id"], [0, 1, 2], ["location"], [0],
// ["address","postcode", "population"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// stations by forceid and neighbourhood
// model
// .get(
// ["neighbourhoodByForceId",
// ["metropolitan"], {from: 0, to: 10}, ["location"], [0],
// ["address","postcode", "population"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// // locations work directly
// model
// .get(
// ["locationsByForceIdAndCode", ["metropolitan"], ["cp"] , 0, ["address","postcode", "population"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data3').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// model
// .get(
// ["forces", {from: 0, to: 5}, ["id", "description", "engagement_methods"], 1, ["url"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data2').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });
// model
// .get(
// ["forces", {from: 0, to: 5}, ["name", "description", "id"]],
// ["forces", {from: 0, to: 5}, ["engagement_methods"], ["url"]])
// .then(function(response) {
// document.getElementById('forces-data3').innerHTML = JSON.stringify(response, null, 2);
// }, function(err) {
// console.log(err);
// document.getElementById('forces-data2').innerHTML = JSON.stringify(err, null, 2);
// // console.log(err['0'].value.message);
// });