-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.cloudkit.js
306 lines (274 loc) · 8.35 KB
/
jquery.cloudkit.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//------------------------------------------------------------------------------
//
// jquery.cloudkit.js source
//
// Copyright (c) 2008, 2009 Jon Crosby http://joncrosby.me
//
// For the complete source with the bundled dependencies,
// run 'rake dist' and use the contents of the dist directory.
//
//------------------------------------------------------------------------------
(function($) {
$.cloudkit = $.cloudkit || {};
//----------------------------------------------------------------------------
// Resource Model
//----------------------------------------------------------------------------
var buildResource = function(collection, spec, metadata) {
var that = {};
var meta = {};
var json = spec;
// return a key that is unique across all local items
var generateId = function() {
return (new Date).getTime() + '-' + Math.floor(Math.random()*10000);
};
var saveFromRemote = function() {
meta = metadata;
meta.id = generateId();
};
that.save = function(callbacks) {
if (!(typeof metadata === 'undefined')) {
return saveFromRemote();
}
$.ajax({
type: 'POST',
url: collection,
data: JSON.stringify(spec),
contentType: 'application/json',
dataType: 'json',
processData: false,
complete: function(response, statusText) {
if (response.status == 201) {
meta = JSON.parse(response.responseText);
meta.id = generateId();
callbacks.success();
} else {
callbacks.error(response.status);
}
}
});
};
that.update = function(spec, callbacks) {
var id = meta.id;
$.ajax({
type: 'PUT',
url: meta.uri,
data: JSON.stringify(spec),
contentType: 'application/json',
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader('If-Match', meta.etag);
},
processData: false,
complete: function(response, statusText) {
if (response.status == 200) {
meta = JSON.parse(response.responseText);
meta.id = id;
json = spec;
callbacks.success();
} else {
// TODO implement default 412 strategy as progressive diff/merge
callbacks.error(response.status);
}
}
});
};
that.destroy = function(callbacks) {
var id = meta.id
$.ajax({
type: 'DELETE',
url: meta.uri,
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader('If-Match', meta.etag);
},
processData: false,
complete: function(response, statusText) {
meta = JSON.parse(response.responseText);
meta.id = id;
if (response.status == 200) {
meta.deleted = true;
callbacks.success();
} else {
callbacks.error(response.status);
}
}
});
};
that.json = function() {
return json;
};
that.id = function() {
return meta.id;
};
that.uri = function() {
return meta.uri;
};
that.isDeleted = function() {
return (meta.deleted == true);
}
return that;
};
//----------------------------------------------------------------------------
// Internal Data Store
//----------------------------------------------------------------------------
var buildStore = function(collection) {
var that = {};
var key = function(resource) {
return collection+resource.id();
};
var persist = function(resource) {
var k = key(resource);
$.data(window, k, resource);
var index = $.data(window, collection+'index') || [];
index.push(k);
$.data(window, collection+'index', index);
};
that.create = function(spec, callbacks) {
resource = buildResource(collection, spec);
resource.save({
success: function() {
persist(resource);
callbacks.success(resource);
},
error: function(status) {
callbacks.error(status);
}
});
};
that.createFromRemote = function(spec, metadata) {
resource = buildResource(collection, spec, metadata);
resource.save();
persist(resource);
return resource;
};
that.all = function(spec) {
// TODO - don't ignore spec
var result = [];
var index = $.data(window, collection+'index');
$(index).each(function(count, id) {
var item = $.data(window, id);
if (!item.isDeleted()) {
result.push(item);
}
});
return result;
};
that.get = function(id) {
return $.data(window, collection+id);
};
that.query = function(spec) {
var jsonObjects = [];
var self = this;
$(this.all()).each(function(index, item) {
json = $.extend(item.json(), {'___id___':item.id()});
jsonObjects.push(json);
});
var query_result = JSONQuery(spec, jsonObjects);
var resources = []
$(query_result).each(function(index, item) {
resources.push(self.get(item['___id___']));
});
return resources;
}
return that;
};
//----------------------------------------------------------------------------
// Private API
//----------------------------------------------------------------------------
var collectionURIs = []; // collection URIs found during boot via discovery
var collections = {}; // local stores, one per remote resource collection
// load remote collection URIs
var loadMeta = function(callbacks) {
$.ajax({
type: 'GET',
url: '/cloudkit-meta',
complete: function(response, statusText) {
data = JSON.parse(response.responseText);
if (response.status == 200) {
collectionURIs = data.uris;
callbacks.success();
} else if (response.status >= 400) {
callbacks.error(response.status);
} else {
callbacks.error('unexpected error');
}
}
});
};
// configure a local collection
var configureCollection = function(collection) {
$.data(window, collection+'index', []);
var name = collection.replace(/^\//, '');
collections[name] = buildStore(collection);
};
// load remote data into local store
var populateCollectionsFromRemote = function(index, callbacks) {
if (index == collectionURIs.length) {
callbacks.success();
return;
}
$.ajax({
type: 'GET',
url: collectionURIs[index]+"/_resolved",
dataType: 'json',
processData: false,
complete: function(response, statusText) {
if (response.status == 200) {
var resources = JSON.parse(response.responseText).documents;
var name = collectionURIs[index].replace(/^\//, '');
for (var i = 0; i < resources.length; i++) {
var resource = resources[i];
collections[name].createFromRemote(
JSON.parse(resource.document),
{
uri: resource.uri,
etag: resource.etag,
last_modified: resource.last_modified
}
);
}
populateCollectionsFromRemote(index+1, callbacks);
} else {
callbacks.error(response.status);
}
}
});
};
// extend jquery
$.fn.extend($.cloudkit, {
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
// setup the local store
boot: function(callbacks) {
collectionURIs = [];
collections = [];
loadMeta({
success: function() {
$(collectionURIs).each(function(index, collection) {
configureCollection(collection);
});
populateCollectionsFromRemote(0, {
success: function() {
callbacks.success();
},
error: function(status) {
callbacks.error(status);
}
});
},
error: function(status) {
callbacks.error(status);
}
});
},
// return all collections
collections: function() {
return collections;
},
// return a specific collection
collection: function(name) {
return this.collections()[name];
}
});
})(jQuery);