RestActions.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. MWF.xAction = MWF.xAction || {};
  2. MWF.require("MWF.xDesktop.Actions.RestActions", null, false);
  3. MWF.xAction.RestActions = MWF.Actions = {
  4. "actions": {},
  5. "get": function(root){
  6. if (this.actions[root]) return this.actions[root];
  7. var actions = null;
  8. var url = o2.session.path+"/xAction/services/"+root+".json";
  9. MWF.getJSON(url, function(json){actions = json;}.bind(this), false, false, false);
  10. if (!MWF.xAction.RestActions.Action[root] && actions.clazz) MWF.require("MWF.xAction.services."+actions.clazz, null, false);
  11. if (!MWF.xAction.RestActions.Action[root]) MWF.xAction.RestActions.Action[root] = new Class({Extends: MWF.xAction.RestActions.Action});
  12. this.actions[root] = new MWF.xAction.RestActions.Action[root](root, actions);
  13. return this.actions[root];
  14. },
  15. "getHost": function(root){
  16. var addressObj = layout.desktop.serviceAddressList[root];
  17. var address = "";
  18. if (addressObj){
  19. address = layout.config.app_protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port);
  20. }else{
  21. var host = layout.desktop.centerServer.host || window.location.hostname;
  22. var port = layout.desktop.centerServer.port;
  23. address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port);
  24. }
  25. return address;
  26. },
  27. "invokeAsync": function(actions, callback){
  28. var len = actions.length;
  29. var parlen = arguments.length-2;
  30. var res = [];
  31. var jsons = new Array(len-1);
  32. var args = arguments;
  33. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  34. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  35. var cb = function(){
  36. if (res.length===len) cbs.apply(this, jsons);
  37. };
  38. var _doError = function(xhr, text, error){
  39. if (xhr.status!=0){
  40. var errorText = error;
  41. if (xhr){
  42. var json = JSON.decode(xhr.responseText);
  43. if (json){
  44. errorText = json.message.trim() || "request json error";
  45. }else{
  46. errorText = "request json error: "+xhr.responseText;
  47. }
  48. }
  49. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  50. }
  51. };
  52. actions.each(function(action, i){
  53. var par = (i<parlen) ? args[i+2] : args[parlen+1];
  54. if (par){
  55. var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
  56. actionArgs.push(function(json){
  57. jsons[i] = json;
  58. res.push(true);
  59. cb();
  60. });
  61. actionArgs.push(function(xhr, text, error){
  62. res.push(false);
  63. if (!cbf){
  64. _doError(xhr, text, error);
  65. }else{
  66. cbf();
  67. }
  68. cb();
  69. });
  70. action.action[action.name].apply(action.action, actionArgs);
  71. }else{
  72. action.action[action.name](function(){
  73. jsons[i] = json;
  74. res.push(true);
  75. cb();
  76. }, function(xhr, text, error){
  77. res.push(false);
  78. if (!cbf){
  79. _doError(xhr, text, error);
  80. }else{
  81. cbf();
  82. }
  83. cb();
  84. });
  85. }
  86. });
  87. }
  88. };
  89. MWF.xAction.RestActions.Action = new Class({
  90. initialize: function(root, actions){
  91. this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
  92. this.action.actions = actions;
  93. Object.each(this.action.actions, function(service, key){
  94. if (service.uri) if (!this[key]) this.createMethod(service, key);
  95. }.bind(this));
  96. },
  97. createMethod: function(service, key){
  98. var jaxrsUri = service.uri;
  99. var re = new RegExp("\{.+?\}", "g");
  100. var replaceWords = jaxrsUri.match(re);
  101. var parameters = [];
  102. if (replaceWords) parameters = replaceWords.map(function(s){
  103. return s.substring(1,s.length-1);
  104. });
  105. this[key] = this.invokeFunction(service, parameters, key);
  106. },
  107. invokeFunction: function(service, parameters, key){
  108. //uri的参数, data(post, put), file(formData), success, failure, async
  109. return function(){
  110. var i = parameters.length-1;
  111. var n = arguments.length;
  112. var functionArguments = arguments;
  113. var parameter = {};
  114. var success, failure, async, data, file;
  115. if (typeOf(functionArguments[0])==="function"){
  116. i=-1;
  117. success = (n>++i) ? functionArguments[i] : null;
  118. failure = (n>++i) ? functionArguments[i] : null;
  119. parameters.each(function(p, x){
  120. parameter[p] = (n>++i) ? functionArguments[i] : null;
  121. });
  122. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  123. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  124. data = (n>++i) ? functionArguments[i] : null;
  125. }else{
  126. data = (n>++i) ? functionArguments[i] : null;
  127. file = (n>++i) ? functionArguments[i] : null;
  128. }
  129. }
  130. async = (n>++i) ? functionArguments[i] : null;
  131. urlEncode = (n>++i) ? functionArguments[i] : true;
  132. }else{
  133. parameters.each(function(p, x){
  134. parameter[p] = (n>x) ? functionArguments[x] : null;
  135. });
  136. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  137. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  138. data = (n>++i) ? functionArguments[i] : null;
  139. }else{
  140. data = (n>++i) ? functionArguments[i] : null;
  141. file = (n>++i) ? functionArguments[i] : null;
  142. }
  143. }
  144. success = (n>++i) ? functionArguments[i] : null;
  145. failure = (n>++i) ? functionArguments[i] : null;
  146. async = (n>++i) ? functionArguments[i] : null;
  147. urlEncode = (n>++i) ? functionArguments[i] : true;
  148. }
  149. return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode});
  150. }.bind(this);
  151. }
  152. });