Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixes: The Observer, Mediator, Facade & Factory Pattern #60

Merged
merged 5 commits into from

2 participants

Matija Marohnić Addy Osmani
Matija Marohnić

As I'm reading the book, I'll send fixes in batches. This is the first one :) First commit represents changes you certainly want, other ones represent changes you probably want.

I'll explain some stuff that might not be obvious, in order of appearance :P

gridUpdate is called before it's defined. Also, I noticed that sometimes you define named functions and sometimes you assign anonymous functions to variables. As far as I know JavaScript, they are pretty much the same, so I suggest you pick one, because it's kind of confusing. For example, gridUpdate is an assigned anonymous function, other grid functions are named.

The function extend is naming it's parameters in the wrong order, because the function seemed like it was extending an extension. Also, the parameter order is unlike other extend functions out there, they all start with the object first, then the extension, so you might wanna change that.

I renamed ratingTemplate to ratingsTemplate because the ID is named that way.

About that Mediator pattern part, I skipped the advanced example because it was to complicated for me, so I don't know how the methods are called there, but I know that method are exposed as capitalized, but used as lowercased.

Addy Osmani
Owner

Thanks for the heads up about these issues. I especially appreciate the accompanying PR. I'll review and merge. Thanks!

Addy Osmani addyosmani merged commit c272672 into from
Matija Marohnić

No problem, thank you for the book, it seems to cover absolutely everything I wanted to know :D

Gordon Chan gchan referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2012
  1. Matija Marohnić

    Fix syntax & logic errors

    silvenon authored
  2. Matija Marohnić

    Fix a semantic error

    silvenon authored
  3. Matija Marohnić

    Correct a comment

    silvenon authored
  4. Matija Marohnić

    Consistency fix

    silvenon authored
  5. Matija Marohnić

    Fix syntax error

    silvenon authored
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 21 deletions.
  1. +22 −21 book/index.html
43 book/index.html
View
@@ -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 ) {
Something went wrong with that request. Please try again.