-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathes_command.js
70 lines (62 loc) · 1.53 KB
/
es_command.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
// If this file is modified copy it to es_client/vendor/
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define(function (require,exports,module) {
var Command = module.exports = function(data){
if(data.substring){
this.initWithString(data);
}
else{
this.initWithHash(data);
}
}
Command.sanitize = function(data_string){
var safe_data_string = data_string.
replace(/>/g, '>').
replace(/</g,'<');
return safe_data_string;
};
Command.parse = function(serialized_msg){
return JSON.parse(serialized_msg);
};
Command.serialize = function(msg){
return JSON.stringify(msg);
};
Command.prototype = {
initWithString: function(data_string){
this.sanitized_data = Command.sanitize(data_string);
this.msg = Command.parse(this.sanitized_data);
},
initWithHash: function(msg){
this.sanitized_data = null;
this.msg = msg;
},
execute: function(obj,cb){
var params = this.getParams();
if(typeof cb === 'function') params.push(cb);
obj[this.getAction()].apply(obj,params);
},
validate: function(){
},
getDataType: function(){
return this.msg.type;
},
getDataId: function(){
return this.msg.id;
},
getAction: function(){
return this.msg.action;
},
getParams: function(){
return this.msg.params;
},
getMessage: function(){
return this.msg;
},
getSerializedMessage: function(){
if(!this.sanitized_data && this.msg){
this.sanitized_data = Command.serialize(this.msg);
}
return this.sanitized_data;
}
};
});