📄 datasource-debug.js
字号:
// Validate if(lang.isNumber(number)) { return number; } else { YAHOO.log("Could not convert data " + lang.dump(oData) + " to type Number", "warn", this.toString()); return null; }},// Backward compatibilityconvertNumber : function(oData) { YAHOO.log("The method YAHOO.util.DataSourceBase.convertNumber() has been" + " deprecated in favor of YAHOO.util.DataSourceBase.parseNumber()", "warn", this.toString()); return DS.parseNumber(oData);},/** * Converts data to type Date. * * @method DataSourceBase.parseDate * @param oData {Date | String | Number} Data to convert. * @return {Date} A Date instance. * @static */parseDate : function(oData) { 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 { YAHOO.log("Could not convert data " + lang.dump(oData) + " to type Date", "warn", this.toString()); return null; }},// Backward compatibilityconvertDate : function(oData) { YAHOO.log("The method YAHOO.util.DataSourceBase.convertDate() has been" + " deprecated in favor of YAHOO.util.DataSourceBase.parseDate()", "warn", this.toString()); 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 = []; YAHOO.log("Cache initialized", "info", this.toString()); } // 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); YAHOO.log("Refreshed cache position of the response for \"" + oRequest + "\"", "info", this.toString()); } // Add a cache flag oResponse.cached = true; break; } } YAHOO.log("The cached response for \"" + lang.dump(oRequest) + "\" is " + lang.dump(oResponse), "info", this.toString()); return oResponse; } } } else if(aCache) { this._aCache = null; YAHOO.log("Cache destroyed", "info", this.toString()); } 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}); YAHOO.log("Cached the response for \"" + oRequest + "\"", "info", this.toString());},/** * Flushes cache. * * @method flushCache */flushCache : function() { if(this._aCache) { this._aCache = []; this.fireEvent("cacheFlushEvent"); YAHOO.log("Flushed the cache", "info", this.toString()); }},/** * 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)) { YAHOO.log("Enabling polling to live data for \"" + oRequest + "\" at interval " + nMsec, "info", this.toString()); var oSelf = this; var nId = setInterval(function() { oSelf.makeConnection(oRequest, oCallback, oCaller); }, nMsec); this._aIntervals.push(nId); return nId; } else { YAHOO.log("Could not enable polling to live data for \"" + oRequest + "\" at interval " + nMsec, "info", this.toString()); }},/** * 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -