forked from dsjoerg/ggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.js
More file actions
141 lines (118 loc) · 4.96 KB
/
resource.js
File metadata and controls
141 lines (118 loc) · 4.96 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
// Modified version of a resource promise implementation for mongolab:
// https://github.com/pkozlowski-opensource/angularjs-mongolab-promise/blob/master/src/mongolabResourceHttp.js
//
// I've become convinced that having a custom $resource-like implementation
// instead of patching ngResource is the way to go, partly due to threads like:
// https://groups.google.com/forum/?fromgroups=#!topic/angular/jKU8Xcc7aFg
//
// Otherwise, we'd have to maintain ngResource patches for a long time..
//
// Now, this brings at the very least one key feature to the table:
//
// * Since the return value of a query is a promise object, we can chain
// on it as much as we want -- for example to show/hide specific loading
// indicators, or to modify the returned result.
// promiseThen here already takes care of our important stuff, so this isn't
// necessary to mold our data.
//
// * Also, since we know when the promise is fulfilled, we know if it's
// actually empty and can choose to not clear out tables while we're loading
// but when they're done loading and came back empty.
gg.factory('$ggResource', ['$http', function ($http) {
function ggResourceFactory(baseUrl) {
var url = baseUrl;
var defaultParams = {};
var promiseThen = function (httpPromise, successcb, errorcb, isArray) {
return httpPromise.then(function (response) {
var result = [];
// When the params key is present, we're namespaced the collection
if(typeof response.data.params != 'undefined') {
_params = response.data.params;
result.page = _params.page;
if (!_.isNumber(result.page)) {
result.page = 1;
}
result.limit = _params.limit;
result.pages = Math.ceil(_params.total / _params.limit);
result.total = _params.total;
// also want those stats, right?
result.stats = response.data.stats;
result.djstats = response.data.djstats;
// Then just put the collection in place.
response.data = response.data.collection;
}
if (isArray) {
for (var i = 0; i < response.data.length; i++) {
result.push(new Resource(response.data[i]));
}
} else {
result = new Resource(response.data);
}
(successcb || angular.noop)(result, response.status, response.headers, response.config);
return result;
}, function (response) {
(errorcb || angular.noop)(undefined, response.status, response.headers, response.config);
return undefined;
});
};
var Resource = function (data) {
angular.extend(this, data);
};
// A get that uses jsonp when gg.state.iecompat is true
Resource.get = function(url, options) {
if(window.gg.state.iecompat) {
options.params = angular.extend(options.params, {callback: 'JSON_CALLBACK'});
return $http.jsonp(url, options)
}
return $http.get(url, options);
}
Resource.all = function (params, cb, errorcb) {
return Resource.query(params, cb, errorcb);
};
Resource.query = function (params, successcb, errorcb) {
var params = angular.isObject(params)&&!angular.equals(params,{}) ? params : {};
var httpPromise = this.get(url, {params:angular.extend({}, defaultParams, params)});
return promiseThen(httpPromise, successcb, errorcb, true);
};
Resource.getById = function (id, successcb, errorcb) {
var httpPromise = $http.get(url + '/' + id, {params:defaultParams});
return promiseThen(httpPromise, successcb, errorcb);
};
Resource.getByObjectIds = function (ids, successcb, errorcb) {
var qin = [];
angular.forEach(ids, function (id) {
qin.push({$oid:id});
});
return Resource.query({_id:{$in:qin}}, successcb, errorcb);
};
//instance methods
Resource.prototype.$id = function () {
if (this._id && this._id.$oid) {
return this._id.$oid;
} else if (this._id) {
return this._id;
}
};
Resource.prototype.$save = function (successcb, errorcb) {
var httpPromise = $http.post(url, this, {params:defaultParams});
return promiseThen(httpPromise, successcb, errorcb);
};
Resource.prototype.$update = function (successcb, errorcb) {
var httpPromise = $http.put(url + "/" + this.$id(), angular.extend({}, this, {_id:undefined}), {params:defaultParams});
return promiseThen(httpPromise, successcb, errorcb);
};
Resource.prototype.$remove = function (successcb, errorcb) {
var httpPromise = $http['delete'](url + "/" + this.$id(), {params:defaultParams});
return promiseThen(httpPromise, successcb, errorcb);
};
Resource.prototype.$saveOrUpdate = function (savecb, updatecb, errorSavecb, errorUpdatecb) {
if (this.$id()) {
return this.$update(updatecb, errorUpdatecb);
} else {
return this.$save(savecb, errorSavecb);
}
};
return Resource;
}
return ggResourceFactory;
}]);