Last active
December 15, 2015 08:59
-
-
Save elijahmanor/5235011 to your computer and use it in GitHub Desktop.
Angry Birds of JavaScript: Blue Bird
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Observer is attached to the #pigs element where impact events are delegated | |
$( "#pigs" ).on( "impact", ".pig", function( e ) { | |
console.log( "I know which pig was impacted: " + e.target.innerHTML ); | |
console.log( "I know where the subscribers are listed: " + e.delegateTarget.id ); | |
console.log( "I can invoke another subscriber if I want!" ); | |
$._data( e.delegateTarget, "events" ).secret[ 0 ].handler( e ); | |
$( this ).text( "Bacon" ); | |
}); | |
$( "#pigs" ).on( "secret", ".pig", function( e ) { | |
console.log( "Shh, I'm hiding. Don't tell anyone..." ); | |
}); | |
// Event is triggers on the .pig element and bubble up to the #pigs element | |
$( ".pig:first" ).trigger( "impact" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var channel = postal.channel(); | |
$( document ).on( "click", ".term", function( e ) { | |
var term = $( this ).text(); | |
e.preventDefault(); | |
$( "input" ).val( term ); | |
channel.publish( "searchTerm.changed", { term: term } ); | |
}); | |
$( "button" ).on( "click", function() { | |
channel.publish( "searchTerm.changed", { term: $( "input" ).val() } ); | |
}); | |
channel.subscribe( "searchTerm.changed", function( data ) { | |
netflix.getTitles( data.term, function( titles ) { | |
channel.publish( "netflix.titles.updated", titles ); | |
}); | |
}); | |
channel.subscribe( "searchTerm.changed", function( data ) { | |
$( ".help-block" ).html( function( index, html ) { | |
return ~html.indexOf( data.term ) ? html : | |
html + ", " + "<a href='#' class='term'>" + data.term + "</a>"; | |
}); | |
}); | |
channel.subscribe( "netflix.titles.updated", function( titles ) { | |
var rows = []; | |
$.each( titles, function( index, result ) { | |
var row = ""; | |
if ( result.Rating && result.ReleaseYear ) { | |
row += "<td>" + result.Name + "</td>"; | |
row += "<td>" + result.Rating + "</td>"; | |
row += "<td>" + result.ReleaseYear + "</td>"; | |
row = "<tr>" + row + "</tr>"; | |
rows.push( row ); | |
} | |
}); | |
$( "table" ).show().find( "tbody" ).html( rows.join( "" ) ); | |
}); | |
window.netflix = { | |
getTitles: function( term, callback ) { | |
var url = "http://odata.netflix.com/Catalog/Titles?$filter=substringof('" + | |
escape( term ) + "',Name)&$callback=callback&$format=json"; | |
$.ajax({ | |
dataType: "jsonp", | |
url: url, | |
jsonpCallback: "callback", | |
success: function( data ) { callback( data.d.results ); } | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var channel = postal.channel(), | |
$lastUpdated = $( "#lastUpdated" ); | |
// Subscribe to all bird.launch messages | |
channel.subscribe( "bird.launch", function( data ) { | |
console.log( "Launch the blue birds at a " + data.angle + " angle!" ); | |
}); | |
// Subscribe to all bird.reset messages | |
channel.subscribe( "bird.reset", function( data ) { | |
console.log( "Resetting blue birds to the catapult." ); | |
}); | |
// Subscribe to all messages that match the bird.* wildcard! | |
channel.subscribe( "bird.*", function( data ) { | |
$lastUpdated.text( moment().format( "MMMM Do YYYY, h:mm:ss a" ) ); | |
}); | |
// Publish some messages with optional data | |
channel.publish( "bird.launch", { angle: 45 } ); | |
channel.publish( "bird.reset" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById( "bird" ) | |
// Native addEventListener attaches observer to the DOM element | |
.addEventListener( "click", function() { console.log( "Catapult!" ); }, false ); | |
$( "#bird" ) | |
// Old school event helpers attaches observer to the DOM element | |
.click( function() { console.log( "Flying through the air..." ); } ) | |
// Old school bind method attaches observer to the DOM element | |
.bind( "click", function() { console.log( "COWABUNGA!" ); } ) | |
// New school 2 parameter on method attaches observer to the DOM element | |
.on( "click", function() { console.log( "Destroy those pesky pigs!" ); } ); | |
// Event is triggered and the list of observers are notified | |
$( "#bird" ).trigger( "click" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( document ).on( "click", ".term", function( e ) { | |
$( "input" ).val( $( this ).text() ); | |
$( "button" ).trigger( "click" ); | |
}); | |
$( "button" ).on( "click", function( e ) { | |
var searchTerm = $( "input" ).val(), | |
url = "http://odata.netflix.com/Catalog/Titles?$filter=substringof('" + | |
escape( searchTerm ) + "',Name)&$callback=callback&$format=json"; | |
$( ".help-block" ).html( function( index, html ) { | |
return e.originalEvent ? | |
html + ", " + "<a href='#' class='term'>" + searchTerm + "</a>" : html; | |
}); | |
$.ajax({ | |
dataType: "jsonp", | |
url: url, | |
jsonpCallback: "callback", | |
success: function( data ) { | |
var rows = []; | |
$.each( data.d.results, function( index, result ) { | |
var row = ""; | |
if ( result.Rating && result.ReleaseYear ) { | |
row += "<td>" + result.Name + "</td>"; | |
row += "<td>" + result.Rating + "</td>"; | |
row += "<td>" + result.ReleaseYear + "</td>"; | |
row = "<tr>" + row + "</tr>"; | |
rows.push( row ); | |
} | |
}); | |
$( "table" ).show().find( "tbody" ).html( rows.join( "" ) ); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment