📄 datasource.js
字号:
var date = null; //Convert to date if(!(oData instanceof Date)) { date = new Date(oData); } else { return oData; } // Validate if(date instanceof Date) { return date; } else { return null; }},// Backward compatibilityconvertDate : function(oData) { return DS.parseDate(oData);}});// Done in separate step so referenced functions are defined./** * Data parsing functions. * @property DataSource.Parser * @type Object * @static */DS.Parser = { string : DS.parseString, number : DS.parseNumber, date : DS.parseDate};// Prototype properties and methodsDS.prototype = {///////////////////////////////////////////////////////////////////////////////// DataSourceBase private properties////////////////////////////////////////////////////////////////////////////////** * Name of DataSource instance. * * @property _sName * @type String * @private */_sName : null,/** * Local cache of data result object literals indexed chronologically. * * @property _aCache * @type Object[] * @private */_aCache : null,/** * Local queue of request connections, enabled if queue needs to be managed. * * @property _oQueue * @type Object * @private */_oQueue : null,/** * Array of polling interval IDs that have been enabled, needed to clear all intervals. * * @property _aIntervals * @type Array * @private */_aIntervals : null,///////////////////////////////////////////////////////////////////////////////// DataSourceBase public properties////////////////////////////////////////////////////////////////////////////////** * Max size of the local cache. Set to 0 to turn off caching. Caching is * useful to reduce the number of server connections. Recommended only for data * sources that return comprehensive results for queries or when stale data is * not an issue. * * @property maxCacheEntries * @type Number * @default 0 */maxCacheEntries : 0, /** * Pointer to live database. * * @property liveData * @type Object */liveData : null,/** * Where the live data is held: * * <dl> * <dt>TYPE_UNKNOWN</dt> * <dt>TYPE_LOCAL</dt> * <dt>TYPE_XHR</dt> * <dt>TYPE_SCRIPTNODE</dt> * <dt>TYPE_JSFUNCTION</dt> * </dl> * * @property dataType * @type Number * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN * */dataType : DS.TYPE_UNKNOWN,/** * Format of response: * * <dl> * <dt>TYPE_UNKNOWN</dt> * <dt>TYPE_JSARRAY</dt> * <dt>TYPE_JSON</dt> * <dt>TYPE_XML</dt> * <dt>TYPE_TEXT</dt> * <dt>TYPE_HTMLTABLE</dt> * </dl> * * @property responseType * @type Number * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN */responseType : DS.TYPE_UNKNOWN,/** * Response schema object literal takes a combination of the following properties: * * <dl> * <dt>resultsList</dt> <dd>Pointer to array of tabular data</dd> * <dt>resultNode</dt> <dd>Pointer to node name of row data (XML data only)</dd> * <dt>recordDelim</dt> <dd>Record delimiter (text data only)</dd> * <dt>fieldDelim</dt> <dd>Field delimiter (text data only)</dd> * <dt>fields</dt> <dd>Array of field names (aka keys), or array of object literals * such as: {key:"fieldname",parser:YAHOO.util.DataSourceBase.parseDate}</dd> * <dt>metaFields</dt> <dd>Object literal of keys to include in the oParsedResponse.meta collection</dd> * <dt>metaNode</dt> <dd>Name of the node under which to search for meta information in XML response data</dd> * </dl> * * @property responseSchema * @type Object */responseSchema : null,///////////////////////////////////////////////////////////////////////////////// DataSourceBase public methods////////////////////////////////////////////////////////////////////////////////** * Public accessor to the unique name of the DataSource instance. * * @method toString * @return {String} Unique name of the DataSource instance. */toString : function() { return this._sName;},/** * Overridable method passes request to cache and returns cached response if any, * refreshing the hit in the cache as the newest item. Returns null if there is * no cache hit. * * @method getCachedResponse * @param oRequest {Object} Request object. * @param oCallback {Object} Callback object. * @param oCaller {Object} (deprecated) Use callback object. * @return {Object} Cached response object or null. */getCachedResponse : function(oRequest, oCallback, oCaller) { var aCache = this._aCache; // If cache is enabled... if(this.maxCacheEntries > 0) { // Initialize local cache if(!aCache) { this._aCache = []; } // Look in local cache else { var nCacheLength = aCache.length; if(nCacheLength > 0) { var oResponse = null; this.fireEvent("cacheRequestEvent", {request:oRequest,callback:oCallback,caller:oCaller}); // Loop through each cached element for(var i = nCacheLength-1; i >= 0; i--) { var oCacheElem = aCache[i]; // Defer cache hit logic to a public overridable method if(this.isCacheHit(oRequest,oCacheElem.request)) { // The cache returned a hit! // Grab the cached response oResponse = oCacheElem.response; this.fireEvent("cacheResponseEvent", {request:oRequest,response:oResponse,callback:oCallback,caller:oCaller}); // Refresh the position of the cache hit if(i < nCacheLength-1) { // Remove element from its original location aCache.splice(i,1); // Add as newest this.addToCache(oRequest, oResponse); } // Add a cache flag oResponse.cached = true; break; } } return oResponse; } } } else if(aCache) { this._aCache = null; } return null;},/** * Default overridable method matches given request to given cached request. * Returns true if is a hit, returns false otherwise. Implementers should * override this method to customize the cache-matching algorithm. * * @method isCacheHit * @param oRequest {Object} Request object. * @param oCachedRequest {Object} Cached request object. * @return {Boolean} True if given request matches cached request, false otherwise. */isCacheHit : function(oRequest, oCachedRequest) { return (oRequest === oCachedRequest);},/** * Adds a new item to the cache. If cache is full, evicts the stalest item * before adding the new item. * * @method addToCache * @param oRequest {Object} Request object. * @param oResponse {Object} Response object to cache. */addToCache : function(oRequest, oResponse) { var aCache = this._aCache; if(!aCache) { return; } // If the cache is full, make room by removing stalest element (index=0) while(aCache.length >= this.maxCacheEntries) { aCache.shift(); } // Add to cache in the newest position, at the end of the array var oCacheElem = {request:oRequest,response:oResponse}; aCache[aCache.length] = oCacheElem; this.fireEvent("responseCacheEvent", {request:oRequest,response:oResponse});},/** * Flushes cache. * * @method flushCache */flushCache : function() { if(this._aCache) { this._aCache = []; this.fireEvent("cacheFlushEvent"); }},/** * Sets up a polling mechanism to send requests at set intervals and forward * responses to given callback. * * @method setInterval * @param nMsec {Number} Length of interval in milliseconds. * @param oRequest {Object} Request object. * @param oCallback {Function} Handler function to receive the response. * @param oCaller {Object} (deprecated) Use oCallback.scope. * @return {Number} Interval ID. */setInterval : function(nMsec, oRequest, oCallback, oCaller) { if(lang.isNumber(nMsec) && (nMsec >= 0)) { var oSelf = this; var nId = setInterval(function() { oSelf.makeConnection(oRequest, oCallback, oCaller); }, nMsec); this._aIntervals.push(nId); return nId; } else { }},/** * Disables polling mechanism associated with the given interval ID. * * @method clearInterval * @param nId {Number} Interval ID. */clearInterval : function(nId) { // Remove from tracker if there var tracker = this._aIntervals || []; for(var i=tracker.length-1; i>-1; i--) { if(tracker[i] === nId) { tracker.splice(i,1); clearInterval(nId); } }},/** * Disables all known polling intervals. * * @method clearAllIntervals */clearAllIntervals : function() { var tracker = this._aIntervals || []; for(var i=tracker.length-1; i>-1; i--) { clearInterval(tracker[i]); } tracker = [];},/** * First looks for cached response, then sends request to live data. * * @method sendRequest * @param oRequest {Object} Request object. * @param oCallback {Object} An object literal with the following properties: * <dl> * <dt><code>success</code></dt> * <dd>The function to call when the data is ready.</dd> * <dt><code>failure</code></dt> * <dd>The function to call upon a response failure condition.</dd> * <dt><code>scope</code></dt> * <dd>The object to serve as the scope for the success and failure handlers.</dd> * <dt><code>argument</code></dt> * <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd> * </dl> * @param oCaller {Object} (deprecated) Use oCallback.scope. * @return {Number} Transaction ID, or null if response found in cache. */sendRequest : function(oRequest, oCallback, oCaller) { // First look in cache var oCachedResponse = this.getCachedResponse(oRequest, oCallback, oCaller); if(oCachedResponse) { DS.issueCallback(oCallback,[oRequest,oCachedResponse],false,oCaller); return null; } // Not in cache, so forward request to live data return this.makeConnection(oRequest, oCallback, oCaller);},/** * Overridable default method generates a unique transaction ID and passes * the live data reference directly to the handleResponse function. This * method should be implemented by subclasses to achieve more complex behavior * or to access remote data. * * @method makeConnection * @param oRequest {Object} Request object. * @param oCallback {Object} Callback object literal. * @param oCaller {Object} (deprecated) Use oCallback.scope. * @return {Number} Transaction ID. */makeConnection : function(oRequest, oCallback, oCaller) { var tId = DS._nTransactionId++; this.fireEvent("requestEvent", {tId:tId, request:oRequest,callback:oCallback,caller:oCaller}); /* accounts for the following cases: YAHOO.util.DataSourceBase.TYPE_UNKNOWN YAHOO.util.DataSourceBase.TYPE_JSARRAY YAHOO.util.DataSourceBase.TYPE_JSON YAHOO.util.DataSourceBase.TYPE_HTMLTABLE YAHOO.util.DataSourceBase.TYPE_XML YAHOO.util.DataSourceBase.TYPE_TEXT */ var oRawResponse = this.liveData; this.handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId); return tId;},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -