RestActions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. MWF.xApplication.Common.Actions = MWF.xApplication.Common.Actions || {};
  2. MWF.xApplication.Common.Actions.RestActions = new Class({
  3. initialize: function(){
  4. this.designAddress = "";
  5. MWF.getJSON("../x_component_Common/Actions/properties.jsp", function(json){
  6. this.actions = json;
  7. }.bind(this), false);
  8. },
  9. listApplicationAddress: function(success, failure){
  10. var url = this.actions.listAddress;
  11. url = this.actions.slotHost+url;
  12. var callback = new MWF.xApplication.Common.Actions.RestActions.Callback(success, failure);
  13. MWF.getJSON(url, callback);
  14. },
  15. getDesignAddress: function(success, failure){
  16. // var name = "x_processplatform_core_designer";
  17. // var url = this.actions.getAddress.replace(/{id}/g, name);
  18. // url = this.actions.slotHost+url;
  19. // var callback = new MWF.process.RestActions.Callback(success, failure, function(data){
  20. // this.designAddress = data.data.url;
  21. // }.bind(this));
  22. // MWF.getJSON(url, callback);
  23. this.designAddress = "http://xa01.zoneland.net:9080/x_processplatform_assemble_designer";
  24. //this.designAddress = "http://localhost:9080/x_processplatform_front_designer";
  25. if (success) success.apply();
  26. },
  27. request: function(success, failure, type, arg){
  28. if (this.designAddress){
  29. this["_"+type](success, failure, arg);
  30. }else{
  31. this.getDesignAddress(function(data){
  32. this["_"+type](success, failure, arg);
  33. }.bind(this), failure);
  34. }
  35. },
  36. _getId: function(success, failure, count){
  37. var url = this.designAddress+this.actions.getId;
  38. url = url.replace(/{count}/g, count);
  39. var callback = new MWF.xApplication.Common.Actions.RestActions.Callback(success, failure);
  40. MWF.getJSON(url, callback);
  41. },
  42. getId: function(count, success, failure){
  43. this.request(success, failure, "getId", count);
  44. },
  45. getUUID: function(){
  46. if (!this.designAddress) this.getDesignAddress();
  47. var url = this.designAddress+this.actions.getId;
  48. url = url.replace(/{count}/g, "1");
  49. var id = "";
  50. var callback = new MWF.xApplication.Common.Actions.RestActions.Callback(function(ids){
  51. id = ids.data[0].id;
  52. }, null);
  53. MWF.getJSON(url, callback, false);
  54. return id;
  55. }
  56. });
  57. MWF.xApplication.Common.Actions.RestActions.Callback = new Class({
  58. initialize: function(success, failure, appendSuccess, appendFailure){
  59. this.success = success;
  60. this.failure = failure;
  61. this.appendSuccess = appendSuccess;
  62. this.appendFailure = appendFailure;
  63. },
  64. onSuccess: function(responseJSON, responseText){
  65. switch(responseJSON.type) {
  66. case "success":
  67. if (this.appendSuccess) this.appendSuccess(responseJSON);
  68. if (this.success) this.success(responseJSON);
  69. break;
  70. case "warn":
  71. MWF.xDesktop.notice("info", {x: "right", y:"top"}, responseJSON.errorMessage.join("\n"));
  72. if (this.appendSuccess) this.appendSuccess(responseJSON);
  73. if (this.success) this.success(responseJSON);
  74. break;
  75. case "error":
  76. this.doError(null, responseText, responseJSON.message);
  77. break;
  78. }
  79. },
  80. onRequestFailure: function(xhr){
  81. this.doError(xhr, "", "");
  82. },
  83. onFailure: function(xhr){
  84. this.doError(xhr, "", "");
  85. },
  86. onError: function(text, error){
  87. this.doError(null, text, error);
  88. },
  89. doError: function(xhr, text, error){
  90. if (this.appendFailure) this.appendFailure(xhr, text, error);
  91. if (this.failure) this.failure(xhr, text, error);
  92. if (!this.failure && !this.appendFailure){
  93. var errorText = error;
  94. if (xhr) errorText = xhr.responseText;
  95. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  96. // throw "request error: "+errorText;
  97. }
  98. }
  99. });