-
Notifications
You must be signed in to change notification settings - Fork 13
/
mongolab.js
37 lines (28 loc) · 1.27 KB
/
mongolab.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
angular.module('mongolabResource', ['ngResource']).factory('$mongolabResource', ['$resource', 'API_KEY', 'DB_NAME', function ($resource, API_KEY, DB_NAME) {
function MmongolabResourceFactory(collectionName) {
var resource = $resource('https://api.mongolab.com/api/1/databases/' + DB_NAME + '/collections/' + collectionName + '/:id',
{ apiKey:API_KEY, id:'@_id.$oid'}, { update:{ method:'PUT' } }
);
resource.getById = function (id, cb, errorcb) {
return resource.get({id:id}, cb, errorcb);
};
resource.prototype.update = function (cb, errorcb) {
return resource.update({id:this._id.$oid}, angular.extend({}, this, {_id:undefined}), cb, errorcb);
};
resource.prototype.saveOrUpdate = function (savecb, updatecb, errorSavecb, errorUpdatecb) {
if (this._id && this._id.$oid) {
return this.update(updatecb, errorUpdatecb);
} else {
return this.$save(savecb, errorSavecb);
}
};
resource.prototype.remove = function (cb, errorcb) {
return resource.remove({id:this._id.$oid}, cb, errorcb);
};
resource.prototype['delete'] = function (cb, errorcb) {
return this.remove(cb, errorcb);
};
return resource;
}
return MmongolabResourceFactory;
}]);