-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
120 lines (93 loc) · 3.97 KB
/
index.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
var VFR= require("clay-vfr")
var Ajax = function(eventName, model, options){
if(eventName == "create") return Ajax.post.call(this, model,options )
else if(eventName == "update") return Ajax.put.call(this, model,options )
else if(eventName == "destroy") return Ajax.del.call(this, model,options )
//Sho
var params = model;
if(eventName == "query") return Ajax.query.call(this, params, options);
else if(eventName == "read") return Ajax.get.call(this, params, options);
else if(eventName == "api") return Ajax.api.call(this, params, options);
}
Ajax.vfr = function(remoteAction, options){
if(typeof remoteAction != "string" ) throw "First Argument should be the Remote Action (string)"
if(!options) options = { escape: false };
var send = VFR( remoteAction, options, options.nullok || false );
return send
}
Ajax.api = function(){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var remoteAction = arguments[0];
var callArgs = []
for (var i = 1; i < arguments.length-1; i++) {
callArgs.push(args[i]);
};
options = arguments[arguments.length-1];
if(typeof remoteAction != "string" ) throw "First Argument should be the Remote Action (string)"
if(options == remoteAction) options = {};
var send = VFR( this.ajax.namespace + remoteAction, options, options.nullok || false );
return send.apply( VFR, JSON.stringify(this.toJSON()) );
}
Ajax.query = function(params, options){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var pctEncodeSpaces = true;
var params = encodeURIComponent(params).replace(/%40/gi, '@').replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, pctEncodeSpaces ? '%20' : '+');
var send = VFR(this.ajax.namespace + ".handleRest" );
return send( "get", "/query?query=" + params , "" )
.then(function(results){
for (var i = results.length - 1; i >= 0; i--) {
results[i].id = results[i].Id
delete results[i].Id;
};
return results;
})
}
Ajax.get = function(id, options){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var send = VFR(this.ajax.namespace + ".handleRest" );
return send( "get", Ajax.generateURL(this) + "/" + id, "" )
.then(function(data){
data.id = data.Id;
delete results[i].Id;
return data;
});
}
Ajax.post = function(model, options){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var _this = this;
var id = this.id;
this.id = null;
var send = VFR(model.ajax.namespace + ".handleRest" );
return send( "post", Ajax.generateURL(model) , JSON.stringify(this.toJSON()) )
.then( function(data){ _this.id = id; return data; } )
}
Ajax.put = function(model, options){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var valuesToSend = JSON.parse(JSON.stringify(this.toJSON())); //ugly hack
var previousAttributes = JSON.parse( model.previousAttributes[this.id] );
for(key in valuesToSend){
if(valuesToSend[key] == previousAttributes[key]){
delete valuesToSend[key];
}
}
var send = VFR(model.ajax.namespace + ".handleRest", {}, true );
return send( "put", Ajax.generateURL(model, this.id ), JSON.stringify(valuesToSend) )
.then( function(data){ return data; } )
}
Ajax.del = function(model, options){
if(!this.ajax.namespace && this.ajax.namespace != "") this.ajax.namespace = "threevot."
var send = VFR(model.ajax.namespace + ".handleRest", {}, true );
return send( "del", Ajax.generateURL(model, this.id ), "" );
}
Ajax.generateURL = function() {
var args, collection, object, path, scope;
object = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
collection = object.className;
args.unshift(collection);
args.unshift(scope);
path = args.join('/');
path = path.replace(/(\/\/)/g, "/");
path = path.replace(/^\/|\/$/g, "");
return "/"+path;
};
module.exports = Ajax;