Permalink
Browse files

Merge branch '1.5.2' of https://github.com/addyosmani/essential-js-de…

…sign-patterns into 1.5.2
  • Loading branch information...
2 parents ce226f6 + 9e361a2 commit f60b2fa92fbf416450986a004df60c8294f9c47b @addyosmani committed Jul 8, 2012
Showing with 12 additions and 12 deletions.
  1. +12 −12 book/index.html
View
@@ -2036,7 +2036,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
// Subscribers listen for topics they have subscribed to and
// invoke a callback function (e.g messageLogger) once a new
// notification is broadcast on that topic
-var subscriber = pubsub.subscribe( "inbox/newMessage", messageLogger );
+var subscription = pubsub.subscribe( "inbox/newMessage", messageLogger );
// Publishers are in charge of publishing topics or notifications of
// interest to the application. e.g:
@@ -2054,7 +2054,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
// We cab also unsubscribe if we no longer wish for our subscribers
// to be notified
-// pubsub.unsubscribe( testSubscription );
+// pubsub.unsubscribe( subscription );
// Once unsubscribed, this for example won't result in our
// messageLogger being executed as the subscriber is
@@ -2217,13 +2217,14 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
// to a list of users who have submitted reviews
$.subscribe( "/new/user", function( e, data ){
- var compiledTemplate;
+ // Pre-compile templates and "cache" them using closure
+ var
+ userTemplate = _.template($( "#userTemplate" ).html()),
+ ratingTemplate = _.template($( "#ratingsTemplate" ).html());
if( data ){
- compiledTemplate = _.template($( "#userTemplate" ).html());
-
- $('#users').append( compiledTemplate( data ));
+ $('#users').append( userTemplate( data ));
}
@@ -2238,9 +2239,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
if( data ){
- compiledTemplate = _.template($( "#ratingsTemplate" ).html());
-
- $( "#ratings" ).append( compiledTemplate( data ) );
+ $( "#ratings" ).append( ratingTemplate( data );
}
@@ -2318,6 +2317,9 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
;(function( $ ) {
+ // Pre-compile template and "cache" it using closure
+ var resultTemplate = _.template($( "#resultTemplate" ).html());
+
// Subscribe to the new search tags topic
$.subscribe( "/search/tags" , function( tags ) {
$( "#searchResults" )
@@ -2327,9 +2329,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
// Subscribe to the new results topic
$.subscribe( "/search/resultSet" , function( results ){
- var compiled_template = _.template($( "#resultTemplate" ).html());
-
- $( "#searchResults" ).append(compiled_template( results ));
+ $( "#searchResults" ).append(resultTemplate( results ));
});

0 comments on commit f60b2fa

Please sign in to comment.