Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #60 from silvenon/1.5.2

Fixes: The Observer, Mediator, Facade & Factory Pattern
  • Loading branch information...
commit c272672b0d6e0695946df2a4538804ccb2f49ded 2 parents 9482e45 + 327131c
@addyosmani authored
Showing with 22 additions and 21 deletions.
  1. +22 −21 book/index.html
View
43 book/index.html
@@ -1662,6 +1662,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
if( this.observerList[i] === obj ){
pointer = i;
}
+ i++;
}
return pointer;
@@ -1678,9 +1679,9 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
// Extend an object with an extension
-function extend( obj, extension ){
- for ( var key in obj ){
- extension[key] = obj[key];
+function extend( extension, obj ){
+ for ( var key in extension ){
+ obj[key] = extension[key];
}
}
@@ -1702,7 +1703,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
};
Subject.prototype.RemoveObserver = function( observer ){
- this.observers.RemoveIndexAt( this.observers.IndexOf( observer, 0 ) );
+ this.observers.RemoveAt( this.observers.IndexOf( observer, 0 ) );
};
Subject.prototype.Notify = function( context ){
@@ -2085,9 +2086,6 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
<pre class="brush: js">
-// Create a subscription to the newDataAvailable topic
-var subscriber = pubsub.subscribe( "newDataAvailable", gridUpdate );
-
// Return the current local time to be used in our UI later
getCurrentTime = function (){
@@ -2121,12 +2119,15 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
gridUpdate = function( topic, data ){
if ( data !== "undefined" ) {
- grid.addGridRow( data );
- grid.updateCounter( data );
+ addGridRow( data );
+ updateCounter( data );
}
};
+// Create a subscription to the newDataAvailable topic
+var subscriber = pubsub.subscribe( "newDataAvailable", gridUpdate );
+
// The following represents updates to our data layer. This could be
// powered by ajax requests which broadcast that new data is available
// to the rest of the application.
@@ -2217,15 +2218,15 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
;(function( $ ) {
+ // Pre-compile templates and "cache" them using closure
+ var
+ userTemplate = _.template($( "#userTemplate" ).html()),
+ ratingsTemplate = _.template($( "#ratingsTemplate" ).html());
+
// Subscribe to the new user topic, which adds a user
// to a list of users who have submitted reviews
$.subscribe( "/new/user", function( e, data ){
- // Pre-compile templates and "cache" them using closure
- var
- userTemplate = _.template($( "#userTemplate" ).html()),
- ratingTemplate = _.template($( "#ratingsTemplate" ).html());
-
if( data ){
$('#users').append( userTemplate( data ));
@@ -2243,7 +2244,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
if( data ){
- $( "#ratings" ).append( ratingTemplate( data );
+ $( "#ratings" ).append( ratingsTemplate( data );
}
@@ -2296,7 +2297,7 @@ <h2 id="observerpatternjavascript">The Observer Pattern</h2>
&lt;input type="text" name="tag" id="query"/&gt;
- &lt;input type="submit' name="submit' value="submit'/&gt;
+ &lt;input type="submit" name="submit" value="submit"/&gt;
&lt;/form&gt;
@@ -2447,8 +2448,8 @@ <h2 id="mediatorpatternjavascript">
};
return {
- Publish: publish,
- Subscribe: subscribe,
+ publish: publish,
+ subscribe: subscribe,
installTo: function( obj ){
obj.subscribe = subscribe;
obj.publish = publish;
@@ -2777,7 +2778,7 @@ <h2 id="mediatorpatternjavascript">
&lt;br /&gt;
&lt;label for=&quot;chatBox&quot;&gt;Message:&lt;/label&gt;
&lt;input id=&quot;chatBox&quot; type=&quot;text&quot;/&gt;
- &lt;button action=&quot;submit&quot;&gt;Chat&lt;/button&gt;
+ &lt;button type=&quot;submit&quot;&gt;Chat&lt;/button&gt;
&lt;/form&gt;
&lt;div id=&quot;chatResult&quot;&gt;&lt;/div&gt;
@@ -3167,7 +3168,7 @@ <h2 id="facadepatternjavascript">The Facade Pattern</h2>
}());
-// Outputs: "running", 10
+// Outputs: "current value: 10" and "running"
module.facade( {run: true, val:10} );
</pre>
@@ -3346,7 +3347,7 @@ <h2 id="factorypatternjavascript">The Factory Pattern</h2>
getVehicle: function ( type, customizations ) {
var Vehicle = types[type];
- return (Vehicle) ? return new Vehicle(customizations) : null;
+ return (Vehicle ? new Vehicle(customizations) : null);
},
registerVehicle: function ( type, Vehicle ) {
Please sign in to comment.
Something went wrong with that request. Please try again.