Angry Birds of JavaScript- Green Bird: Mocking
3 min read


A diabolical herd of pigs stole all of the front-end architecture from an innocent flock of birds and now they want it back!

A team of special agent hero birds will attack those despicable pigs until they recover what is rightfully theirs, front-end JavaScript architecture!

Will the birds be successful in the end? Will they defeat their bacon flavored foe? Let's find out together in another nail biting episode of Angry Birds of JavaScript!

Check out the series introduction post for a list of all the birds and their attack powers.

Previous Attacks

Green Bird Attacks

In this post we will take a look at the Green Bird that can penetrate all of those hard to reach places and will mock and spy those stealing swine right where it hurts!. Slowly, one by one, the birds will take back what it theirs to keep!

What Was Stolen by the Pigs?

For the most part the birds are all front-end web developers only and don't focus on the back-end much at all. As a result the birds have a symbiotic relationship with the Water Buffalo. The Buffalo write the back-end of the application and the birds develop the front-end. The problem with this arrangement is that while the back-end is being developed the birds are left twiddling their feathers until the Buffalo are finished. However, once the back-end is done the pressure is on the birds to deliver while the Buffalo sit idle by the watering hole in the shade. Thankfully, a while back, a Green Bird proposed the idea of mocking the back-end services so they could make progress on the front-end while they waited for the Buffalo to finish their work! The Green Bird introduced a couple of handy libraries to make mocking a much easier process.

However, during a recent invasion the pigs stole the birds' mocking libraries! As a result, one of the Green Birds has been tasked to reclaim what has been stolen. He will use his overwhelming power of trickery to help destroy the pigs in order to take back what is theirs.

The Twitter Application

We are going to take a look at a simple Twitter application that gets the tweets from a specific username. Why Twitter? Well, the Angry Birds have a special relationship with Twitter, especially the Blue Bird ;)

The following application grabs data from Twitter using JSONP. I thought about using Backbone.js to write the little application, but thought that might be overkill to introduce the mocking concept. You will also notice I am not using a templating engine and that is on purpose. Another Angry Bird will be introducing that concept ;)

(function( twitter, $, undefined ) {
var channel = =,
$selection = null;
twitter.selector = null;
twitter.init = function( selector ) {
twitter.selector = selector;
channel.subscribe( "tweets.available", twitter.displayTweets );
twitter.displayTweets = function( tweets ) {
var $list = $( "<ul/ >" ),
$location = $selection || $( twitter.selector );
// This would be better suited for a templating engine,
// but that's for another Angry Bird ;)
$.each( tweets || {}, function( index, tweet ) {
var html = "<i>" + moment( tweet.created_at ).fromNow() + "</i>: ";
html += "<b>" + + "</b> - ";
html += tweet.text;
html += tweet.retweeted ? " <i class='icon-repeat'></i>" : "";
html += tweet.favorited ? " <i class='icon-star'></i>" : "";
$( "<li />", { html: html }).appendTo( $list );
$location.append( $list.children() );
twitter.getTweets = function( userName, count ) {
var url = _.string.sprintf( URL_TEMPLATE, {
userName: userName,
count: count || 5
url: url,
dataType: "jsonp",
success: function( tweets ) {
channel.publish( "tweets.available", tweets );
}( window.twitter = window.twitter || {}, jQuery ));
twitter.init( ".tweets" );
$( document ).on( "click", "button", function( e ) {
var $input = $( this ).closest( "form" ).find( "input" );
twitter.getTweets( $input.val() || "elijahmanor" );
view raw twitter.js hosted with ❤ by GitHub
The above code snippet is running in the below embedded jsFiddle. Feel free to run the code and launch the editor to play around with it.

Twitter Application using

The data that comes back from Twitter looks something like the following screenshot...

Mocking Static Data

What if Twitter goes down, is unstable, or you just want to test your application without having internet access? This is where being able to mock the back-end service can be really helpful. Thankfully we can use a jQuery library called Mockjax. All you need to do is call \$.mockjax and provide a URL that you want to listen for and a response that you want to be returned. The following is an example of mocking the call to and passing back some static data.

url: "*",
responseTime: 750,
responseText: [
{ id: 0, created_at: "Mon Apr 11 8:00:00 +0000 2012", text: "Test Tweet 1",
favorited: false, retweeted: false, user: { name: "User 1" } },
{ id: 1, created_at: "Mon Apr 11 9:00:00 +0000 2012", text: "Test Tweet 2",
favorited: true, retweeted: true, user: { name: "User 2" } },
{ id: 2, created_at: "Mon Apr 11 10:00:00 +0000 2012", text: "Test Tweet 3",
favorited: false, retweeted: true, user: { name: "User 3" } },
{ id: 3, created_at: "Mon Apr 11 11:00:00 +0000 2012", text: "Test Tweet 4",
favorited: true, retweeted: false, user: { name: "User 4" } },
{ id: 4, created_at: "Mon Apr 11 12:00:00 +0000 2012", text: "Test Tweet 5",
favorited: true, retweeted: true, user: { name: "User 5" } }
Not only is this pretty cool and can be helpful for developing the front-end independent from the back-end, but it also very handy when writing Unit Tests that use Ajax.

The above code snippet is running in the below embedded jsFiddle. Feel free to run the code and launch the editor to play around with it.

Twitter Application using Mockjax

The data that comes back from Mockjax looks something like the following screenshot...

Mocking Dynamic Semi-Random Data

One of the downsides of this technique is that generally I am pretty unimaginative and lazy when making static data examples. I typically end up have the same object and just increment some of the values by 1 or something. That is fine and all, but it is a nuisance, it takes time, and it doesn't really give you an idea of what the UI could look like. Thankfully there is another nice library for that called mockJSON. You provide a template of what you want your data to look like and then you give it some metadata about what types of fields you want, how many, etc... The following is how I rewrote the previous example, but will randomly generate anywhere from 5 to 10 twitter objects to be displayed.

url: "*",
responseTime: 750,
response: function() {
var data = $.mockJSON.generateFromTemplate({
"tweets|5-10": [{
"id|+1": 0,
"created_at": "Mon Apr 11 @TIME_HH:@TIME_MM:@TIME_SS +0000 2012",
"text": "@LOREM_IPSUM",
"favorited|0-1": false,
"retweeted|0-1": false,
"user": { "name": "@MALE_FIRST_NAME @LAST_NAME" }
this.responseText = data.tweets;

The above code snippet is running in the below embedded jsFiddle. Feel free to run the code and launch the editor to play around with it.

Twitter Application using Mockjax & mockJSON

The data that comes back from Mockjax with mockJSON looks something like the following screenshot...


The following is a simple Angry Birds clone using boxbox, a framework for the box2dweb JavaScript physics library, written by Bocoup's Greg Smith.

Press the space bar to launch the Green Bird and you can also use the arrow keys.


It can be difficult to develop both the front-end and back-end independently. Thankfully there are some techniques and libraries today that can enable the front-end to develop and prototype separate from the back-end progress. The mocking techniqiue with static data can also be helpful when Unit Testing your code as well.

There are many other front-end architecture techniques that have been stolen by the pigs. Tune in next time as the next Angry Bird takes its revenge! Dun, dun, daaaaaaa!

Edit post on GitHub

Angry Birds of JavaScript- Orange Bird: Templating
Angry Birds of JavaScript- White Bird: Linting