📄 restdatasource.js
字号:
/*
* Isomorphic SmartClient
* Version 6.5 (2008-04-30)
* Copyright(c) 1998-2007 Isomorphic Software, Inc. All rights reserved.
* "SmartClient" is a trademark of Isomorphic Software, Inc.
*
* licensing@smartclient.com
*
* http://smartclient.com/license
*/
// Visit http://www.smartclient.com for more information on Isomorphic SmartClient //> @class RestDataSource// The RestDataSource implements the 4 core DataSource operations using a simple protocol of// XML or JSON requests and responses sent over HTTP, which can be easily fulfilled by any HTTP// server technology.// <P>// RestDataSource is named for the// +externalLink{http://www.google.com/search?hl=en&q=REST+HTTP,REST} (REpresentational State// Transfer) pattern, which in brief says that simple messages passed over HTTP is a sufficient// protocol for many web applications, without the need for further protocols such as WSDL or// SOAP.// <P>// A RestDataSource is used just like a normal DataSource. RestDataSources are configured,// using the general-purpose databinding facilities of DataSources, to expect a particular// format for responses and to send requests in a specific format. These request and// response formats represent Isomorphic's recommended best practices for binding SmartClient// to backends which do not already support a similar, pre-existing request and response// format.// <P>// Full source code to the RestDataSource is provided (see // /examples/databinding/RestDataSource.js), intended for possible further modification and an// extended example of how to use SmartClient's general databinding engine.// <P>// RestDataSource is typically used with PHP, Ruby, Python, Perl or custom server technologies,// and represents an alternative to installing the SmartClient Server in a Java technology// stack, or using +link{group:wsdlBinding,WSDL-based binding} with .NET or other WSDL-capable// technologies.// <P>// <span style="font-weight:bold;font-size:16px;">Examples</span>// <p>// <b>XML formatted responses:</b>// <P>// RestDataSource expects a response to like the following in response to a "fetch" request:// <pre>// <response>// <status>0</status>// <data>// <record>// <field1>value</field1>// <field2>value</field2>// </record>// <record>// <field1>value</field1>// <field2>value</field2>// </record>// </data>// </response>// </pre>// The status element indicates whether the fetch operation was successful // (see +link{group:statusCodes}).// <P>// The data element contains a list of record nodes, each of which represents a record returned// by the server. Additional elements for totalRows returned, startRow and endRow may// also be included.// <P>// Note: for a more compact format, simple field values may be specified on record // nodes directly as attributes - in this case a record element might be structured like this:// <pre>// <record field1="value" field2="value" />// </pre>// <p>// Successful "add" or "update" request responses are similar in format - in this case the data// element would be expected to contain a single record object containing the details of the// record, as saved on the server.// <P>// The response from a "remove" operation would again include status and data elements, but in// this case, only the primary key field value(s) of the removed record would be expected to be // present under the data element.// <p>// If a validation failure occurred on the server, the response would// have status set to +link{RPCResponse.STATUS_VALIDATION_ERROR} [<code>-4</code> by default],// and any validation errors could be included as per-field sub-elements of an "errors"// element.// <P>// Note that in this case the response would not be expected to contain any data element. // A response showing a validation error might look like this:// <pre>// <response>// <status>-4</status>// <errors>// <field1>// <errorMessage>A validation error occurred for this field</errorMessage>// </field1>// </errors>// </response>// </pre>// <p>// <b>JSON formatted responses:</b>// <P>// JSON format responses are expected to contain the same data / meta-data as XMLresponses,// encapsulated a simple object with a <code>"response"</code> attribute.<br>// The response to a "fetch" request would therefore have this format:<br>// <pre>// { response:// { status:0,// data:[// {field1:"value", field2:"value"},// {field1:"value", field2:"value"}// ]// }// }// </pre>// The structure successful for "add", "update" and "remove" responses would be similar, though// the data array would be expected to contain the single edited record only.<br>// For a remove, only the value for the primaryKey field[s] would be required.// <p>// If an error occurred on the server, this would be indicated in the <code>status</code> // attribute. For a validation error, the status would be set to // +link{RPCResponse.STATUS_VALIDATION_ERROR} [<code>-4</code> by default], and errors would// be specified in the <code>errors</code> attribute of the response. For example:// <pre>// { response:// { status:-4,// errors: // { field1:{errorMessage:"A validation error on field1"},// field2:{errorMessage:"A validation error on field2"}// }// }// }// </pre>// An array of errors may also be returned for a single field, like this:// <pre>// { response:// { status:-4,// errors: // { field1:[// {errorMessage:"First error on field1"},// {errorMessage:"Second error on field1"}// ]// }// }// }// </pre>// <b>Server inbound data formats</b>// <P>// The format of data sent to the server is determined by the +link{OperationBinding.dataProtocol}// specified for the operation. Request data is sent as parameters if the format is // specified as <code>"getParams"</code> or <code>"postParams"</code>.// <P>// In this case, the parameters sent to the server will consist of the DSRequest's data, and any// parameters explicitly specified on the DSRequest object (as +link{RPCRequest.params}.<br> // If +link{RestDataSource.sendMetaData} is true, the DSRequest meta // data properties will also be present as parameters, prefixed with // +link{RestDataSource.metaDataPrefix}.// <P>// Example URL constructed with the metaDataPrefix set to <code>"_"</code> (the default):// <p>// <code>// <i>[dataURL]</i>?field1=value1&_operationType=fetch&_startRow=0&_endRow=50&_sortBy=-field2&_dataSource=dsName// </code>// <p>// In this case the server would be able to separate the request's data from the meta data // via the <code>"_"</code> prefix.// <P>// If data is sent to the server via the <code>"postMessage"</code> dataProtocol, the data will// be sent as a serialized XML message. In this case the created XML document will contain // nodes for each relevant property of the request (including a <code>"data"</code> node // containing the request's data.// <P>// An example of an XML message might look like this:// <pre>// <data>// <countryDS>// <countryCode>US</countryCode>// <countryName>Edited Value</countryName>// <capital>Edited Value</capital>// <continent>Edited Value</continent>// </countryDS>// </data>// <dataSource>countryDS</dataSource>// <operationType>update</operationType>// </pre>// The +link{restDataSource.operationBindings,default OperationBindings} for a RestDataSource// specify dataProtocol as "getParams" for the fetch operation, and "postParams" for update,// add and remove operations.// <P>// <b>Hierachical (Tree) data:</b>// <P>// To create a hierachical DataSource, in the DataSource's <code>fields</code> array, a field // must be specified as the parent id field - the field which will contain a pointer to the// id of each node's parent. // This can be achieved by setting the +link{DataSourceField.foreignKey} and the // +link{DataSourceField.rootValue} attributes on the field definition. For example:// <pre>// RestDataSource.create({// ID:"supplyItem",// fields : [// {name:"itemId", type:"sequence", primaryKey:true},// {name:"parentId", type:"integer", foreignKey:"supplyItem.itemId", rootValue:0},// ...// ]// });// </pre>// Tree Data is then treated on the server as a flat list of records linked by parent id.<br>// Tree data is typically displayed using a dataBound +link{class:TreeGrid} component. TreeGrids// automatically create a +link{class:ResultTree} data object, which requests data directly// from the DataSource. ResultTrees load data on demand, only requesting currently visible // (open) nodes from the server. This is handled by including a specified value for the parent // id field in the request criteria.<br>// To implement a standard load-on-demand tree RestDataSource back end, you should therefore // simply return the set of nodes that match the criteria passed in. // For example, if your DataSource was defined as the "supplyItem" code snippet above, // a fetch request for all children of a node with <code>itemId</code> set to <code>12</code> // would have <code>"parentId"</code> set to <code>12</code> in the request criteria.// A valid response would then contain all the records that matched this criteria. For example:// <pre>// <response>// <status>0</status>// <data>// <record>// <itemId>15</itemId>// <parentId>12</parentId>// </record>// <record>// <itemId>16</itemId>// <parentId>12</parentId>// </record>// </data>// </response>// </pre>// The structure of responses for Add, Update and Delete type requests will be the // same regardless of whether the data is hierachical. However you should be aware that // the underlying data storage may need to be managed slightly differently in some cases.<br>// Specifically, Add and Update operations may change the structure of the tree by returning a // new parent id field value for the modified node. Depending on how your data is stored you // may need to include special back-end logic to handle this.<br>// Also, if a user deletes a folder within a databound tree, any children of that folder will // also be dropped from the tree, and can be removed from the back-end data storage.<br>// Note: For a general overview of binding components to Tree structured data, see // +link{group:treeDataBinding, Tree Databinding}.// // @treeLocation Client Reference/Data Binding// @visibility external// @example restEditSave//<isc.defineClass("RestDataSource", "DataSource");isc.RestDataSource.addProperties({ serverType:"generic", //> @attr RestDataSource.dataFormat (string : "xml" : IR) // Expected format for server responses. RestDataSources handle <code>"json"</code> and // <code>"xml"</code> format responses by default. See class overview documentation for // examples of responses in each format. // @visibility external //< dataFormat:"xml", //> @attr RestDataSource.xmlRecordXPath (string : "/response/data/*" : IR) // <code>recordXPath</code> mapping to the data node of XML returned by the server. // Applies if this.dataFormat is set to <code>"xml"</code>.<br> // The default value will pick up data from a response structured as follows:<br> // <pre> // <response> // <status>0</status> // <data> // <record> // <field1>value</field1> // <field2>value</field2> // </record> // <record> // <field1>value</field1> // <field2>value</field2> // </record> // </data> // </response>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -