Async call data
Sync async ajax call data into one list.
by Tim Sommer
JavaScript
//calls the provided url and returns the handler
function fetchBlogPosts(feed) {
return $.ajax({
type: "GET",
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(feed),
dataType: 'json',
error: function () {
alert('Unable to load feed, Incorrect path or invalid feed');
},
success: function (xml) {},
});
}
//sorts an array on date
function sortfunction(a, b) {
a = a.date;
b = b.date;
if (a < b) return 1;
else if (a > b) return -1;
else return 0;
}
//The viewmodel is returned by whole.
(
function feedViewModel() {
//entry for binding on html
var Entry = function (title, creator, link, body, pubDate, date) {
this.title = title;
this.creator = creator;
this.link = link;
this.body = body;
this.pubDate = pubDate;
this.date = date;
}
//initialize & call the handlers and store them in the result array
var results = [];
var feeds = [
"http://www.timsommer.be/blog/feed/",
"http://blog.voltje.be/feed/",
"http://petermorlion.blogspot.com/feeds/posts/default"];
for (f in feeds) {
console.debug('fetching blogposts for feed: ' + feeds[f]);
results.push(fetchBlogPosts(feeds[f]));
}
//Wait for all async calls to be completed
$.when.apply(this, results).done(function () {
var blogPosts = [];
// fetch the result from each arg (succes function in ajax call)
for (var i = 0; i < arguments.length; i++) {
//fetch the entries from the argument parameter
values = arguments[i][0].responseData.feed.entries;
var mappedEntries = $.map(values, function (item) {
var d = new Date(item.publishedDate);
var...