| 1 |
dojo.provide("dojox.wire.ml.Invocation"); |
| 2 |
|
| 3 |
dojo.require("dojox.wire.ml.Action"); |
| 4 |
|
| 5 |
dojo.declare("dojox.wire.ml.Invocation", dojox.wire.ml.Action, { |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
object: "", |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
method: "", |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
topic: "", |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
parameters: "", |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
result: "", |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
error: "", |
| 35 |
|
| 36 |
_run: function(){ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if(this.topic){ |
| 50 |
var args = this._getParameters(arguments); |
| 51 |
try{ |
| 52 |
dojo.publish(this.topic, args); |
| 53 |
this.onComplete(); |
| 54 |
}catch(e){ |
| 55 |
this.onError(e); |
| 56 |
} |
| 57 |
}else if(this.method){ |
| 58 |
var scope = (this.object ? dojox.wire.ml._getValue(this.object) : dojo.global); |
| 59 |
if(!scope){ |
| 60 |
return; |
| 61 |
} |
| 62 |
var args = this._getParameters(arguments); |
| 63 |
var func = scope[this.method]; |
| 64 |
if(!func){ |
| 65 |
func = scope.callMethod; |
| 66 |
if(!func){ |
| 67 |
return; |
| 68 |
} |
| 69 |
args = [this.method, args]; |
| 70 |
} |
| 71 |
try{ |
| 72 |
var connected = false; |
| 73 |
if(scope.getFeatures){ |
| 74 |
var features = scope.getFeatures(); |
| 75 |
if((this.method == "fetch" && features["dojo.data.api.Read"]) || |
| 76 |
(this.method == "save" && features["dojo.data.api.Write"])){ |
| 77 |
var arg = args[0]; |
| 78 |
if(!arg.onComplete){ |
| 79 |
arg.onComplete = function(){}; |
| 80 |
} |
| 81 |
|
| 82 |
this.connect(arg, "onComplete", "onComplete"); |
| 83 |
if(!arg.onError){ |
| 84 |
arg.onError = function(){}; |
| 85 |
} |
| 86 |
|
| 87 |
this.connect(arg, "onError", "onError"); |
| 88 |
connected = true; |
| 89 |
} |
| 90 |
} |
| 91 |
var r = func.apply(scope, args); |
| 92 |
if(!connected){ |
| 93 |
if(r && (r instanceof dojo.Deferred)){ |
| 94 |
var self = this; |
| 95 |
r.addCallbacks( |
| 96 |
function(result){self.onComplete(result);}, |
| 97 |
function(error){self.onError(error);} |
| 98 |
); |
| 99 |
}else{ |
| 100 |
this.onComplete(r); |
| 101 |
} |
| 102 |
} |
| 103 |
}catch(e){ |
| 104 |
this.onError(e); |
| 105 |
} |
| 106 |
} |
| 107 |
}, |
| 108 |
|
| 109 |
onComplete: function(result){ |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if(this.result){ |
| 119 |
dojox.wire.ml._setValue(this.result, result); |
| 120 |
} |
| 121 |
if(this.error){ |
| 122 |
dojox.wire.ml._setValue(this.error, ""); |
| 123 |
} |
| 124 |
}, |
| 125 |
|
| 126 |
onError: function(error){ |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
if(this.error){ |
| 135 |
if(error && error.message){ |
| 136 |
error = error.message; |
| 137 |
} |
| 138 |
dojox.wire.ml._setValue(this.error, error); |
| 139 |
} |
| 140 |
}, |
| 141 |
|
| 142 |
_getParameters: function(args){ |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if(!this.parameters){ |
| 154 |
|
| 155 |
return args; |
| 156 |
} |
| 157 |
var parameters = []; |
| 158 |
var list = this.parameters.split(","); |
| 159 |
if(list.length == 1){ |
| 160 |
var parameter = dojox.wire.ml._getValue(dojo.trim(list[0]), args); |
| 161 |
if(dojo.isArray(parameter)){ |
| 162 |
parameters = parameter; |
| 163 |
}else{ |
| 164 |
parameters.push(parameter); |
| 165 |
} |
| 166 |
}else{ |
| 167 |
for(var i in list){ |
| 168 |
parameters.push(dojox.wire.ml._getValue(dojo.trim(list[i]), args)); |
| 169 |
} |
| 170 |
} |
| 171 |
return parameters; |
| 172 |
} |
| 173 |
}); |