-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone-deployd.js
More file actions
47 lines (38 loc) · 1.19 KB
/
backbone-deployd.js
File metadata and controls
47 lines (38 loc) · 1.19 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
'use strict';
(function(root){
// Keep track of Backbone.sync
root.Backbone.originalSync = root.Backbone.sync;
// Map Backbone method name to deployd SDK functions name
var methodToFunction = {
"read":"get",
"create":"post",
"update":"put",
"delete":"del"
};
// Deployd support a single function with result,error as parameters
var defaultCallback = function(result,error){
if (error) options.error(error);
else options.success(result);
}
root.Backbone.sync = function(method,model,options) {
// Default base args.
var args = [defaultCallback];
var functionName = methodToFunction[method];
// Set dpd call parameters depending on the method indeed.
if (functionName == "get") {
if (options.query) args = [options.query].concat(args);
if (model.id) args = [model.id].concat(args);
}
else if (functionName == "post") {
args = [model.toJSON()].concat(args);
}
else if (functionName == "put") {
args = [model.id,model.toJSON()].concat(args);
}
else if (functionName == "del") {
args = [model.id].concat(args);
}
// Get good function name from the map, and call with good parameters
dpd[model.resourceName][functionName].apply(dpd,args);
};
})(window);