Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #43 from artvolk/1.5.2

Pub/sub typo fix, templates cached
  • Loading branch information...
commit 9e361a21c97697b89dc9b9f8e2ab36cc652a2e44 2 parents 289b19e + ddda72c
@addyosmani authored
Showing with 12 additions and 12 deletions.
  1. +12 −12 book/index.html
View
24 book/index.html
@@ -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 ));
});
Please sign in to comment.
Something went wrong with that request. Please try again.