forked from maccman/holla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperrpc.js
More file actions
105 lines (87 loc) · 2.22 KB
/
Copy pathsuperrpc.js
File metadata and controls
105 lines (87 loc) · 2.22 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
//= require <superclass>
//= require <superevent>
var SuperRPC = new SuperClass;
SuperRPC.extend(SuperEvent);
SuperRPC.extend({
endpoint: "/ria/rpc",
// Usage: invoke(klass, method, *args)
invoke: function(){
var args = jQuery.makeArray(arguments);
var klass = args.shift();
var method = args.shift();
var callback = args.pop();
if(typeof(callback) != "function" &&
typeof(callback) != "undefined"){
callback = null;
args.push(callback);
}
var message = {
klass:klass,
method:method,
args:args
};
this.log("Invoking", klass, method, args);
jQuery.ajax({
url: this.endpoint,
type: "POST",
contentType: "application/json; charset=utf-8",
success: this.callback(callback),
dataType: "json",
data: JSON.stringify(message),
beforeSend: this.proxy(function(xhr){
this.trigger("beforeSend", xhr);
})
});
},
// Private methods
callback: function(callback){
return(this.proxy(function(result){
// We're not throwing the error, so it can be caught
if (result && result.error)
this.log("RPC Error -", result.name, result.message);
if (callback)
callback.call(callback, result);
}));
},
trace: false,
log: function(){
if ( !this.trace ) return;
if (typeof console == "undefined") return;
var args = jQuery.makeArray(arguments);
args.unshift("(App)");
console.log.apply(console, args);
}
});
if (typeof SuperModel != "undefined") {
SuperModel.extend({
invoke: function(){
var args = jQuery.makeArray(arguments);
args.unshift(this.className);
SuperRPC.invoke.apply(SuperRPC, args);
}
})
SuperModel.include({
createRemote: function(callback){
this._class.invoke(
"create",
this.attributes(),
callback
);
},
updateRemote: function(callback){
this._class.invoke(
"update",
this.id,
this.attributes(),
callback
);
},
destroyRemote: function(callback){
this._class.invoke(
"destroy",
this.id,
callback
);
}
});
}