| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- MWF.xAction = MWF.xAction || {};
- MWF.require("MWF.xDesktop.Actions.RestActions", null, false);
- MWF.xAction.RestActions = MWF.Actions = {
- "actions": {},
- "get": function(root){
- if (this.actions[root]) return this.actions[root];
- var actions = null;
- var url = o2.session.path+"/xAction/services/"+root+".json";
- MWF.getJSON(url, function(json){actions = json;}.bind(this), false, false, false);
- if (!MWF.xAction.RestActions.Action[root] && actions.clazz) MWF.require("MWF.xAction.services."+actions.clazz, null, false);
- if (!MWF.xAction.RestActions.Action[root]) MWF.xAction.RestActions.Action[root] = new Class({Extends: MWF.xAction.RestActions.Action});
- this.actions[root] = new MWF.xAction.RestActions.Action[root](root, actions);
- return this.actions[root];
- },
- "getHost": function(root){
- var addressObj = layout.desktop.serviceAddressList[root];
- var address = "";
- if (addressObj){
- address = layout.config.app_protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port);
- }else{
- var host = layout.desktop.centerServer.host || window.location.hostname;
- var port = layout.desktop.centerServer.port;
- address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port);
- }
- return address;
- }
- };
- MWF.xAction.RestActions.Action = new Class({
- initialize: function(root, actions){
- this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
- this.action.actions = actions;
- Object.each(this.action.actions, function(service, key){
- if (service.uri) if (!this[key]) this.createMethod(service, key);
- }.bind(this));
- },
- createMethod: function(service, key){
- var jaxrsUri = service.uri;
- var re = new RegExp("\{.+?\}", "g");
- var replaceWords = jaxrsUri.match(re);
- var parameters = [];
- if (replaceWords) parameters = replaceWords.map(function(s){
- return s.substring(1,s.length-1);
- });
- this[key] = this.invokeFunction(service, parameters, key);
- },
- invokeFunction: function(service, parameters, key){
- //uri的参数, data(post, put), file(formData), success, failure, async
- return function(){
- var i = parameters.length-1;
- var n = arguments.length;
- var functionArguments = arguments;
- var parameter = {};
- var success, failure, async, data, file;
- if (typeOf(functionArguments[0])==="function"){
- i=-1;
- success = (n>++i) ? functionArguments[i] : null;
- failure = (n>++i) ? functionArguments[i] : null;
- parameters.each(function(p, x){
- parameter[p] = (n>++i) ? functionArguments[i] : null;
- });
- if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
- if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
- data = (n>++i) ? functionArguments[i] : null;
- }else{
- data = (n>++i) ? functionArguments[i] : null;
- file = (n>++i) ? functionArguments[i] : null;
- }
- }
- async = (n>++i) ? functionArguments[i] : null;
- }else{
- parameters.each(function(p, x){
- parameter[p] = (n>x) ? functionArguments[x] : null;
- });
- if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
- if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
- data = (n>++i) ? functionArguments[i] : null;
- }else{
- data = (n>++i) ? functionArguments[i] : null;
- file = (n>++i) ? functionArguments[i] : null;
- }
- }
- success = (n>++i) ? functionArguments[i] : null;
- failure = (n>++i) ? functionArguments[i] : null;
- async = (n>++i) ? functionArguments[i] : null;
- }
- return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure});
- }.bind(this);
- }
- });
|