-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestng.js
More file actions
405 lines (329 loc) · 9.7 KB
/
Copy pathrestng.js
File metadata and controls
405 lines (329 loc) · 9.7 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var module = angular.module('restng',[]);
// Return an object whose prototype is <object>.
function child_of(object) {
var Surrogate = function() {};
Surrogate.prototype = object;
return new Surrogate();
}
// Extend function shamelessly stolen from Backbone.js:
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
var extend = function(parent,protoProps, staticProps) {
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
};
module.factory(
'RestNG',
function($http,$rootScope) {
/* The basis of RestNG is the Resource object. Any Resource
* object presented outside of the scope of this file is designed
* to be an "empty shell", all of whose instance variables
* correspond to fields that are a part of the model. Through the
* prototype chain, many other features are accessible. Every
* resource object has two semi-hidden sub-objects, _net and
* _config. This looks like:
*
* Resource X:
* {
* id : 123
* name : "Daniel Staudigel"
* birthday : "March 26, 1987"
*
* __proto__ : { // note, __proto__ is not directly accessible in IE
* _net : {
* connected : true; // flag of whether the model has been synced to the server
* error : null; // non-nil string if a network error occurred
* },
* _config : {
* url : "/users/45"
* },
* __proto__ : ResourcePrototype
* }
* }
*
* Because value lookup goes up the prototype chain, you can
* access these things easily:
*
* alert(rsrcX._net.connected); // -> displays "true"
*
* But when you JSON.stringify or expose the object to Angular,
* you get something more usable:
*
* { id : 123 ,
* name : "Daniel Staudigel" ,
* birthday : "March 26, 1987" }
*
*/
// CreateResource creates an intermediate prototype inbetween the
// passed-in one and the returned object. This houses _net and
// _config objects which are themselves prototypes to their
// parents. This way _net and _config behave well (inheriting
// values from above), but are also stored out of the way of the
// child object. Further, modification of values inside these
// objects works for just the one resource, without those values
// propagating up the prototype chain.
function CreateResourcePrototype(prototype) {
var intermediate_resource = child_of(prototype || ResourceRootPrototype);
intermediate_resource._meta = {};
intermediate_resource._net = child_of(prototype._net);
intermediate_resource._config = child_of(prototype._config);
return intermediate_resource;
}
function CreateResource(prototype) {
return child_of(CreateResourcePrototype(prototype));
}
function Configuration() {
}
Configuration.prototype = {
baseUrl : '/',
idAttribute : 'id',
childPrototype : null // This must inherit from ResourceRoot
};
var rootConfig = new Configuration();
function Net() {};
Net.prototype = {
reset : function() {
delete this.error;
delete this.errorCode;
}
};
var rootNet = new Net();
var ResourceRootPrototype = {
__root : ResourceRootPrototype, // so it's accessible from everywhere
_config : rootConfig,
_net : rootNet,
setId : function(id) {
this[this._config.idAttribute] = id;
},
getId : function() {
return this[this._config.idAttribute];
},
withConfig : function(callback) {
var child = CreateResource(this);
callback(child._config);
return child;
},
extend : function(prototype) {
var c = child_of(this);
_.extend(c,prototype);
return c;
},
onSyncOnce : function(callback) {
if(this._net.active) {
var cbs = this._config.thenCallbacks = this._config.thenCallbacks || [];
cbs.push(callback);
} else {
callback();
}
},
onSync : function(callback) {
var c = this._config.syncCallbacks = this._config.syncCallbacks || [];
if(callback) {
c.push(callback);
} else {
_.each(c,function(d) { d(); });
if(this._config.thenCallbacks) {
var cb = null;
while((cb = this._config.thenCallbacks.pop())) {
cb(this);
}
}
}
},
url : function() {
var id = this.getId();
return this._config.baseUrl + (id?"/"+id:"");
},
one : function(path,id,prototype) {
var child = CreateResource(this || prototype);
child._config.baseUrl += path;
if(id)
child.setId(id);
return child;
},
all : function(path,prototype) {
var p = CreateResourcePrototype(ResourceRootPrototype);
p._config = child_of(this._config);
var child = child_of(p);
var url = this.url();
if(url.length > 0 && url[url.length-1] != '/')
url += '/';
child._config.baseUrl = url + path;
// alert("Createing a child ="+child._config.baseUrl);
child._config.childPrototype = prototype || ResourceRootPrototype;
return child;
},
cache : function(key,generator) {
if(!this._meta[key])
this._meta[key] = generator.call(this);
return this._meta[key];
},
_http : function(hash) {
return $http(hash);
},
find : function(id) {
// the ID Attribute in the child model:
var idAttr = this._config.childPrototype._config.idAttribute;
if(this[id]) {
return this[id];
}
var proto = CreateResourcePrototype(this._config.childPrototype);
proto._config.baseUrl = this.url();
// only create a new one if we don't have one at a
// given ID:
var c = this[id] || child_of(proto);
c[idAttr] = id;
this[id] = c;
return c;
},
length : function() {
return this._config.length;
},
_add : function(elem) {
var idAttr = this._config.childPrototype._config.idAttribute;
var id = elem[idAttr];
var c = this.find(id);
_.extend(c,elem);
},
_net_error : function(data,status,header,config) {
var _net = this._net;
var error = data;
try {
error = angular.fromJson(data).error;
} catch(x) {
error = data;
}
$rootScope.$broadcast("error","Net error : "+status+" for "+config.url);
_net.errorCode = status;
_net.error = error;
_net.active = false;
this.onSync();
},
del : function() {
var that = this;
var _net = this._net;
_net.reset();
_net.active = true;
this._http({ method : "DELETE",
url : this.url() })
.success(
function(data,status,header,config) {
for(var k in this) {
that[k] = "DELETED";
}
that.onSync();
}
)
.error(
function() {
that._net_error.apply(that,arguments);
}
);
;
},
get : function() {
var that = this;
var _net = this._net;
_net.reset();
_net.active = true;
this._http({ method : "GET",
url : this.url() })
.success(
function(data,status,header,config) {
_net.connected = true;
_net.active = false;
if(_.isArray(data)) {
_.each(
data,function(elem) {
that._add(elem);
});
that._config.length = data.length;
} else {
_.extend(that,data);
$rootScope.$broadcast("client-sync");
}
that.onSync();
})
.error(function() { that._net_error.apply(that,arguments); } );
return this;
},
post : function(object) {
var that = this;
this._http({ method : "POST",
url : this.url(),
data : JSON.stringify(object) })
.success(
function(data,status,header,config) {
var _net = that._net;
_net.connected = true;
_net.active = false;
try {
var json = angular.fromJson(data);
that._add(data);
} catch(x) {
_net.errorCode = -1;
_net.error = "Error parsing: "+x;
console.error("Parse error ",x);
_net.active = false;
that.onSync();
alert("Error in network proc: "+x);
}
that.onSync();
})
.error(function() { that._net_error.apply(that,arguments); } );
},
toJSON : function() {
var out = {};
_.each(this,function(value,key) {
if(key.indexOf('$') != 0)
out[key] = value;
});
return out;
},
put : function() {
var that = this;
var _net = this._net;
_net.reset();
_net.active = true;
this._http({ method : "PUT",
url : this.url(),
data : JSON.stringify(this)
})
.success(
function(data,status,header,config) {
_net.connected = true;
_net.active = false;
that.onSync();
})
.error(
function(data,status,header,config) {
that._net_error.apply(that,arguments);
});
return this;
}
};
return ResourceRootPrototype;
// return { $get : function() { return ResourceRootPrototype } };
});