// You can add data from asynchronous sources whenever it becomes available. generateSampleData().forEach(function (item) { list.push(item); });
// Returns an array of sample data that can be added to the application's // data list. function generateSampleData() { var itemContent = "<p>Curabitur class … "; var itemDescription = "Item Description: Pellente…"; var groupDescription = "Group Description: Lorem …"; … return sampleItems; }
var list = new WinJS.Binding.List();
var groupedItems = list.createGrouped( function groupKeySelector(item) { return item.group.key; }, function groupDataSelector(item) { return item.group; } );
WinJS.Namespace.define("Data", { items: groupedItems, groups: groupedItems.groups, getItemReference: getItemReference, getItemsFromGroup: getItemsFromGroup, resolveGroupReference: resolveGroupReference, resolveItemReference: resolveItemReference });
Important : in this article, in order to simplify the code, we will use only RSS feeds for data retrieval, however, adding support for Atom feeds should not be difficult, since they have a similar structure and the key difference will be in addressing the desired data fields.
var blogs = [ { key: "ABlogging", url: "http://blogs.windows.com/windows/b/bloggingwindows/rss.aspx", title: 'Blogging Windows', rsstitle: 'tbd', updated: 'tbd', dataPromise: null }, { key: "BExperience", url: 'http://blogs.windows.com/windows/b/windowsexperience/rss.aspx', title: 'Windows Experience', rsstitle: 'tbd', updated: 'tbd', dataPromise: null }, { key: "CExtreme", url: 'http://blogs.windows.com/windows/b/extremewindows/rss.aspx', title: 'Extreme Windows', rsstitle: 'tbd', updated: 'tbd', dataPromise: null }];
function getBlogPosts(postsList) { blogs.forEach(function (feed) { // Promise feed.dataPromise = WinJS.xhr( { url: feed.url } ).then( function (response) { if (response) { var syndicationXML = response.responseXML || (new DOMParser()).parseFromString(response.responseText, "text/xml"); processRSSFeed(syndicationXML, feed, postsList); } } ); }); return postsList; }
Note : for the blogs we use there is no need for additional verification that we received a response in the form of XML (the presence of responseXML), however, in general, this is incorrect: some blogs due to incorrect settings of the server / engine give the RSS feed as text content, which needs to be further processed if we want to work with it as with an XML file.
function processRSSFeed(articleSyndication, feed, postsList) { // feed.rsstitle = articleSyndication.querySelector("rss > channel > title").textContent; // var published = articleSyndication.querySelector("rss > channel > item > pubDate").textContent; // var date = new Date(published); var dateFmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter( "day month.abbreviated year.full"); var blogDate = dateFmt.format(date); feed.updated = ": " + blogDate; // getItemsFromRSSFeed(articleSyndication, feed, postsList); }
function getItemsFromRSSFeed(articleSyndication, feed, postsList) { var posts = articleSyndication.querySelectorAll("item"); // var length = posts.length; for (var postIndex = 0; postIndex < length; postIndex++) { var post = posts[postIndex]; // var postPublished = post.querySelector("pubDate").textContent; var postDate = new Date(postPublished); var monthFmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month.abbreviated"); var dayFmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("day"); var yearFmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("year.full"); var timeFmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shorttime"); var postContent = toStaticHTML(post.querySelector("description").textContent); var postItem = { index: postIndex, group: feed, // title: post.querySelector("title").textContent, // postDate: postDate, month: monthFmt.format(postDate).toUpperCase(), day: dayFmt.format(postDate), year: yearFmt.format(postDate), time: timeFmt.format(postDate), // content: postContent, // link: post.querySelector("link").textContent }; postsList.push(postItem); } }
Note : in general, working with RSS feeds from uncontrolled sources needs to be carefully monitored (and investigated) the data obtained. In practice, part of the data on the required fields may be absent, so before extracting text content (textContent), you must make sure that the previous operation returned a non-null value. In some cases, the full content of the post may be hidden behind elements of encoded or full-text or be completely absent. There are also cases when the server gives the date in the wrong format, with the result that the standard parser throws an exception.
list = getBlogPosts(list);
Source: https://habr.com/ru/post/163443/
All Articles