Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 64 lines (53 sloc) 1.95 KB
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="../satisfaction.js"></script>
<script>
var start = new Date(),
log = window.console ? window.console.log : function () {};
// Needs to be called only after:
// 1. All JSONP results are returned
// 2. The DOM is ready
function mashupTweets(byJohn, byRyan, byJames) {
var aggregated = byJohn.concat(byRyan).concat(byJames);
$.each(aggregated, function (idx, tweet) {
$("<li>" + tweet.text + "</li>").appendTo($("#tweets"));
});
var elapsed = new Date() - start;
log("Elapsed: " + elapsed);
}
// Define how we'll celebrate our satisfaction
// -- it's by displaying an awesome Twitter mashup of course!
var satisfaction = new Satisfaction(mashupTweets);
// Tag each ordinary callback as a satisfaction source by simply wrapping them
// with Satisfaction.from()
// Get John's tweets
$.getJSON(
"http://twitter.com/status/user_timeline/jeresig.json?count=3&callback=?",
satisfaction.from(function (tweets) {
log("Got latest 3 tweets by John");
return tweets;
}));
// Get Ryan's tweets
$.getJSON(
"http://twitter.com/status/user_timeline/ryah.json?count=3&callback=?",
satisfaction.from(function (tweets) {
log("Got latest 3 tweets by Ryan");
return tweets;
}));
// Get James's tweets
$.getJSON(
"http://twitter.com/status/user_timeline/jrburke.json?count=3&callback=?",
satisfaction.from(function (tweets) {
log("Got latest 3 tweets by James");
return tweets;
}));
// Wait for DOM ready
$(satisfaction.from(function () {
log("DOM ready");
}));
// Could have been written simply as:
// $(satisfaction.from());
// We're done setting expectations. Now let's wait to get satisfaction.
satisfaction.guarantee();
</script>
<h1>A more satisfactory handling of asynchronous events</h1>
<ul id="tweets"></ul>