📄 atomreadstore.js
字号:
element: this.doc.getElementsByTagName("feed")[0], store: this, _attribs: {} }; this._parseItem(this._feedMetaData); } return this._feedMetaData._attribs[attribute] || defaultValue; }, _initItem: function(item){ // summary: // Initializes an item before it can be parsed. if(!item._attribs){ item._attribs = {}; } }, _fetchItems: function(request, fetchHandler, errorHandler) { // summary: // Retrieves the items from the Atom XML document. var url = this._getFetchUrl(request); if(!url){ errorHandler(new Error("No URL specified.")); return; } var localRequest = (!this.sendQuery ? request : null); // use request for _getItems() var _this = this; var docHandler = function(data){ _this.doc = data; var items = _this._getItems(data, localRequest); var query = request.query; if(query) { if(query.id) { items = dojo.filter(items, function(item){ return (_this.getValue(item, "id") == query.id); }); } else if(query.category){ items = dojo.filter(items, function(entry) { var cats = _this.getValues(entry, "category"); if(!cats){ return false; } return dojo.some(cats, "return item.term=='"+query.category+"'"); }); } } if (items && items.length > 0) { fetchHandler(items, request); } else { fetchHandler([], request); } }; if (this.doc) { docHandler(this.doc); }else{ var getArgs = { url: url, handleAs: "xml"//, // preventCache: true }; var getHandler = dojo.xhrGet(getArgs); getHandler.addCallback(docHandler); getHandler.addErrback(function(data){ errorHandler(data, request); }); } }, _getFetchUrl: function(request){ if(!this.sendQuery){ return this.url; } var query = request.query; if(!query){ return this.url; } if(dojo.isString(query)){ return this.url + query; } var queryString = ""; for(var name in query){ var value = query[name]; if(value){ if(queryString){ queryString += "&"; } queryString += (name + "=" + value); } } if(!queryString){ return this.url; } //Check to see if the URL already has query params or not. var fullUrl = this.url; if(fullUrl.indexOf("?") < 0){ fullUrl += "?"; }else{ fullUrl += "&"; } return fullUrl + queryString; }, _getItems: function(document, request) { // summary: // Parses the document in a first pass if(this._items){ return this._items; } var items = []; var nodes = []; if(document.childNodes.length < 1){ this._items = items; console.log("dojox.data.AtomReadStore: Received an invalid Atom document. Check the content type header"); return items; } var feedNodes = dojo.filter(document.childNodes, "return item.tagName && item.tagName.toLowerCase() == 'feed'"); var query = request.query; if(!feedNodes || feedNodes.length != 1){ console.log("dojox.data.AtomReadStore: Received an invalid Atom document, number of feed tags = " + (feedNodes? feedNodes.length : 0)); return items; } nodes = dojo.filter(feedNodes[0].childNodes, "return item.tagName && item.tagName.toLowerCase() == 'entry'"); if(request.onBegin){ request.onBegin(nodes.length); } for(var i = 0; i < nodes.length; i++){ var node = nodes[i]; if(node.nodeType != 1 /*ELEMENT_NODE*/){ continue; } items.push(this._getItem(node)); } this._items = items; return items; }, close: function(/*dojo.data.api.Request || keywordArgs || null */ request){ // summary: // See dojo.data.api.Read.close() },/* internal API */ _getItem: function(element){ return { element: element, store: this }; }, _parseItem: function(item) { var attribs = item._attribs; var _this = this; var text, type; function getNodeText(node){ var txt = node.textContent || node.innerHTML || node.innerXML; if(!txt && node.childNodes[0]){ var child = node.childNodes[0]; if (child && (child.nodeType == 3 || child.nodeType == 4)) { txt = node.childNodes[0].nodeValue; } } return txt; } function parseTextAndType(node) { return {text: getNodeText(node),type: node.getAttribute("type")}; } dojo.forEach(item.element.childNodes, function(node){ var tagName = node.tagName ? node.tagName.toLowerCase() : ""; switch(tagName){ case "title": attribs[tagName] = { text: getNodeText(node), type: node.getAttribute("type") }; break; case "subtitle": case "summary": case "content": attribs[tagName] = parseTextAndType(node); break; case "author": var nameNode ,uriNode; dojo.forEach(node.childNodes, function(child){ if(!child.tagName){ return; } switch(child.tagName.toLowerCase()){ case "name":nameNode = child;break; case "uri": uriNode = child; break; } }); var author = {}; if(nameNode && nameNode.length == 1){ author.name = getNodeText(nameNode[0]); } if(uriNode && uriNode.length == 1){ author.uri = getNodeText(uriNode[0]); } attribs[tagName] = author; break; case "id": attribs[tagName] = getNodeText(node); break; case "updated": attribs[tagName] = dojo.date.stamp.fromISOString(getNodeText(node) );break; case "published": attribs[tagName] = dojo.date.stamp.fromISOString(getNodeText(node));break; case "category": if(!attribs[tagName]){ attribs[tagName] = []; } attribs[tagName].push({scheme:node.getAttribute("scheme"), term: node.getAttribute("term")}); break; case "link": if(!attribs[tagName]){ attribs[tagName] = []; } var link = { rel: node.getAttribute("rel"), href: node.getAttribute("href"), type: node.getAttribute("type")}; attribs[tagName].push(link); if(link.rel == "alternate") { attribs["alternate"] = link; } break; default: break; } }); }, _unescapeHTML : function(text) { //Replace HTML character codes with their unencoded equivalents, e.g. ’ with ' text = text.replace(/’/m , "'").replace(/″/m , "\"").replace(/</m,">").replace(/>/m,"<").replace(/&/m,"&"); return text; }, _assertIsItem: function(/* item */ item){ // summary: // This function tests whether the item passed in is indeed an item in the store. // item: // The item to test for being contained by the store. if(!this.isItem(item)){ throw new Error("dojox.data.AtomReadStore: Invalid item argument."); } }, _assertIsAttribute: function(/* attribute-name-string */ attribute){ // summary: // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store. // attribute: // The attribute to test for being contained by the store. if(typeof attribute !== "string"){ throw new Error("dojox.data.AtomReadStore: Invalid attribute argument."); } }});dojo.extend(dojox.data.AtomReadStore,dojo.data.util.simpleFetch);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -