⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restdatasource.js

📁 javascript 很酷的类库
💻 JS
📖 第 1 页 / 共 2 页
字号:
    // </pre>    // @visibility external    //<    xmlRecordXPath:"/response/data/*",        //> @attr RestDataSource.jsonRecordXPath    (string : "/response/data" : IR)    // <code>recordXPath</code> mapping to the data node of json returned by the server.    // Applies if this.dataFormat is set to <code>"json"</code><br>    // The default value will pick up data from a response structured as follows:<br>    // <pre>    // {response:    //  {status:0,    //   data:[    //      {field1:"value", field2:"value"},    //      {field1:"value", field2:"value"}    //   ]    // }    // </pre>    // @visibility external    //<    jsonRecordXPath:"/response/data",        // Overrid init to pick up these paths    init : function () {        this.recordXPath = (this.dataFormat == "xml" ? this.xmlRecordXPath : this.jsonRecordXPath);        return this.Super("init", arguments);    },        //> @attr RestDataSource.operationBindings (Array of OperationBinding : [...] : IR)    // RestDataSource OperationBindings set to specify default dataProtocol per operationType.    // Default databindings are:    // <pre>    // [ {operationType:"fetch", dataProtocol:"getParams"},    //   {operationType:"add", dataProtocol:"postParams"},    //   {operationType:"remove", dataProtocol:"postParams"},    //   {operationType:"update", dataProtocol:"postParams"} ];    // </pre>    // @visibility external    //<    operationBindings:[       {operationType:"fetch", dataProtocol:"getParams"},       {operationType:"add", dataProtocol:"postParams"},       {operationType:"remove", dataProtocol:"postParams"},       {operationType:"update", dataProtocol:"postParams"}    ],        //> @attr RestDataSource.dataURL  (string : null : IR)    // Default URL to contact to fulfill all DSRequests.      // RestDataSources also allow per-operationType dataURLs to be set via    // <ul>    // <li>+link{RestDataSource.fetchDataURL}</li>    // <li>+link{RestDataSource.addDataURL}</li>     // <li>+link{RestDataSource.updateDataURL}</li>    // <li>+link{RestDataSource.removeDataURL}</li>    // </ul>    // @visibility external    //<        //> @attr RestDataSource.fetchDataURL    (string : null : IR)    // Custom dataURL for fetch type operations    // @visibility external    //<    //> @attr RestDataSource.updateDataURL    (string : null : IR)    // Custom dataURL for update type operations    // @visibility external    //<        //> @attr RestDataSource.addDataURL    (string : null : IR)    // Custom dataURL for add type operations    // @visibility external    //<        //> @attr RestDataSource.removeDataURL    (string : null : IR)    // dataURL for fetch type operations    // @visibility external    //<        //> @attr RestDataSource.sendMetaData (boolean : true : IR)    // Should  operation meta data be included when assmebling parameters to send     // to the server? If true, meta data parameters will be prefixed with the     // +link{RestDataSource.metaDataPrefix}.<br>    // Applies to operations where OperationBinding.dataProtocol is set to     // <code>"getParams"</code> or <code>"postParams"</code> only.    // @visibility external    //<    sendMetaData:true,    //> @attr RestDataSource.metaDataPrefix   (string : "_" :IR)    // I +link{RestDataSource.sendMetaData} is true, this attribute is used to specify    // the prefix to apply to 'meta data' properties when assembling parameters to send to the     // server.  Applies to operations where OperationBinding.dataProtocol is set to     // <code>"getParams"</code> or <code>"postParams"</code> only.    // @visibility external    //<    metaDataPrefix:"_",        // getDataURL()     // overridden to respect fetchDataURL et al.    getDataURL : function (dsRequest) {         var type = dsRequest.operationType;        if (type == "fetch" && this.fetchDataURL != null)             return this.fetchDataURL;        if (type == "update" && this.updateDataURL != null)            return this.updateDataURL;        if (type == "add" && this.addDataURL != null)             return this.addDataURL;        if (type == "remove" && this.removeDataURL != null)            return this.removeDataURL;        return this.Super("getDataURL", arguments);   },    //> @method RestDataSource.transformRequest()    // RestDataSource.transformRequest() implemented to format request meta-data properties     // for transmitting to the server.<br>    // See +link{class:RestDataSource, RestDataSource overview} for a description of the     // standard formatting applied to requests.    // @visibility external    //<    transformRequest : function (dsRequest) {        var protocol = this.getDataProtocol(dsRequest);        // "postMessage": Post data as XML serialized message        if (protocol == "postMessage") {                    if (this.dataFormat == "json") {                this.logWarn('DataSource dataProtocol specified for operation as "postMessage". ' +                             'Data will be sent to the server as a serialized xml message' +                             ' even though dataFormat is set to "json"');             }            var params = {                dataSource:this.getID(),                operationType:dsRequest.operationType,                operationID:dsRequest.operationID,                startRow:dsRequest.startRow,                endRow:dsRequest.endRow,                sortBy:dsRequest.sortBy,                textMatchStyle:dsRequest.textMatchStyle,                clientContext:dsRequest.clientContext            };            var ds = isc.DataSource.create({                fields:[                    {name:"data", multiple:true, type:this.getID()},                    {name:"oldValues", type:this.getID()}                ]            });                        params.data = dsRequest.data;            params.oldValues = dsRequest.oldValues;                        return ds.xmlSerialize(params, null, null, "request");                    // "getParams" / "postParams": HTTP Parameters format                    } else {                        if (protocol != "getParams" && protocol != "postParams") {                this.logWarn("RestDataSource operation:"+ dsRequest.operationID + ", of type "                              + dsRequest.operationType + " has dataProtocol specified as '"                              + protocol + "'. Supported protocols are 'postParams', 'getParams' "                             + "and 'postMessage' only. Defaulting to 'getParams'.");                dsRequest.dataProtocol = 'getParams';                                         }                        // All fields passed in as 'data' will be available directly as parameters            // Also include any explicit parameters present on the dsRequest            var params = isc.addProperties({}, dsRequest.data, dsRequest.params);                    // Attach meta data parameters to the transaction            if (this.sendMetaData) {                if (!this.parameterNameMap) {                    var map = {};                                        map[this.metaDataPrefix  + "operationType"] = "operationType";                    map[this.metaDataPrefix + "operationID"] = "operationID";                    map[this.metaDataPrefix + "startRow"] = "startRow";                    map[this.metaDataPrefix + "endRow"] = "endRow";                    map[this.metaDataPrefix + "sortBy"] = "sortBy";                    map[this.metaDataPrefix + "textMatchStyle"] = "textMatchStyle";                    map[this.metaDataPrefix + "oldValues"] = "oldValues";                        this.parameterNameMap = map;                }                                // Meta data will be available as parameters with the metaDataPrefix applied                for (var parameterName in this.parameterNameMap) {                    var value = dsRequest[this.parameterNameMap[parameterName]];                    if (value != null) params[parameterName] = value;                }                params[this.metaDataPrefix + "dataSource"] = this.getID();            }            return params;        }                    },        // Helper method to verify the status returned by the server is valid    getValidStatus : function (status) {        if (isc.isA.String(status)) {            if (parseInt(status) == status) status = parseInt(status);            else {               status = isc.DSResponse[status];                if (status == null) {                    this.logWarn("Unable to map response code: " + status                                  + " to a DSResponse code, setting status to DSResponse.STATUS_FAILURE.");                    status = isc.DSResponse.STATUS_FAILURE;                }            }        }        if (status == null) status = isc.DSResponse.STATUS_SUCCESS;        return status;    },        //> @method RestDataSource.transformResponse()    // RestDataSource.transformResponse() implemented to extract data and meta-data properties    // from the XML Response provided by the server.<br>    // See +link{class:RestDataSource, RestDataSource overview} for a description of the     // standard data format expected to returned from the server.    // @visibility external    //<    transformResponse : function (dsResponse, dsRequest, data) {        if (this.dataFormat == "json") {            var rawResponse = data.response || {};                    dsResponse.status = this.getValidStatus(rawResponse.status);                 // if the status is a validation error, convert the errors from XML            if (dsResponse.status == isc.DSResponse.STATUS_VALIDATION_ERROR) {                var errors = rawResponse.errors;                // Handle being returned an array of errors (per row) or a single error object                // for the modified row.                if (isc.isAn.Array(errors)) {                    if (errors.length > 1) {                        this.logWarn("server returned an array of errors - ignoring all but the first one");                    }                    errors = errors[0];                   }                dsResponse.errors = errors;            // handle being passed a failure status with 'data' being an error string to display            } else if (dsResponse.status < 0) {                dsResponse.data = rawResponse.data;            }                          if (rawResponse.totalRows != null) dsResponse.totalRows = rawResponse.totalRows;            if (rawResponse.startRow != null) dsResponse.startRow = rawResponse.startRow;            if (rawResponse.endRow != null) dsResponse.endRow = rawResponse.endRow;                    } else {            dsResponse.status = this.getValidStatus(data.selectString("//status"));            // if the status is a validation error, convert the errors from XML            if (dsResponse.status == isc.DSResponse.STATUS_VALIDATION_ERROR) {                var errors = data.selectNodes("//errors");                errors = isc.xml.toJS(errors);                if (errors.length > 1) {                    this.logWarn("server returned an array of errors - ignoring all but the first one");                }                errors = errors[0];                   dsResponse.errors = errors;            // handle being passed a raw response where 'data' is an error string to display                                        } else if (dsResponse.status < 0) {                dsResponse.data = data.selectString("//data");            }                          var totalRows = data.selectNumber("//totalRows");            if (totalRows != null) dsResponse.totalRows = totalRows;                        var startRow = data.selectNumber("//startRow");            if (startRow != null) dsResponse.startRow = startRow;                        var endRow = data.selectNumber("//endRow");            if (endRow != null) dsResponse.endRow = endRow;        }                return dsResponse;    }});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -