-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backbone.cfc
125 lines (84 loc) · 2.28 KB
/
Backbone.cfc
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
component {
APPLICATION.users = APPLICATION.users ?: [
{id = 1, firstName = "Adrian", lastName = "Lynch", dob: "1901-12-12"},
{id = 2, firstName = "Anthony", lastName = "Cull", dob: null}
];
function init() {
return THIS;
}
remote function resetUsers() {
APPLICATION.delete("users");
}
function getUsers() {
return APPLICATION.users;
}
function getUser(id) {
requestedUser = null;
APPLICATION.users.each(function(user) {
if (user.id EQ id) {
requestedUser = user;
break;
}
});
return requestedUser;
}
function insertUser(user) {
user.id = APPLICATION.users.len() + 1;
APPLICATION.users.append(user);
return getUser(user.id);
}
function updateUser(user) {
// Apply the passed user attributes to the existing user
APPLICATION.users.each(function(u) {
if (u.id EQ user.id) {
userToModify = u;
break;
}
});
user.each(function(key, value) {
userToModify[key] = value;
});
return getUser(user.id);
}
function deleteUser(user) {
APPLICATION.users.each(function(u) {
if (u.id EQ user.id) {
u = null;
break;
}
});
}
remote function users() {
return getUsers();
}
remote function user() {
/*
An end-point for a user on the server.
A GET request with an ID returns the given user
A PUT request with and ID will update the given user with the supplied data
A POST request will insert a new user with the supplied data and return the new user
A DELETE request will delete the given user
*/
verb = GetHTTPRequestData().method;
user = DeserializeJSON(GetHTTPRequestData().content);
if (verb EQ "GET") {
rtn = getUser();
} else if (verb EQ "POST") {
rtn = insertUser(user);
} else if (verb EQ "PUT") {
rtn = updateUser(user);
} else if (verb EQ "DELETE") {
rtn = deleteUser(user);
} else {
throw "No valid method found in request";
}
return rtn;
}
}
/*
NOTES:
- All script, no tags
- New elvis operator ?: acts as: if IsDefined("xxx") [evaluate this] else [evaluate this] - Removed
- Case of structure keys are now maintained by setting a flag in Railo Admin. This stops myStruct.myKey from ending up as myStruct.MYKEY when it gets serialised
- Array.filter() takes a function which returns a boolean. If true, the current array item is returned
*/