You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: backbone-fundamentals.md
+9-7
Original file line number
Diff line number
Diff line change
@@ -1535,7 +1535,9 @@ The `set()` method available for Collections can also be used for "smart" updati
1535
1535
1536
1536
// Define a model of type 'Beatle' with a 'job' attribute
1537
1537
var Beatle = Backbone.Model.extend({
1538
-
job: 'musician'
1538
+
defaults: {
1539
+
job: 'musician'
1540
+
}
1539
1541
});
1540
1542
1541
1543
// Create models for each member of the Beatles
@@ -3790,7 +3792,7 @@ The outline for this section looks like this:
3790
3792
3791
3793
Download and install node.js from nodejs.org. The node package manager (npm) will be installed as well.
3792
3794
3793
-
Downloadand install MongoDB from mongodb.org. There are detailed installation guides [on the website](http://docs.mongodb.org/manual/installation/).
3795
+
Download, install, and run MongoDB from mongodb.org (you need Mongo to be running to store data in a Mongo database). There are detailed installation guides [on the website](http://docs.mongodb.org/manual/installation/).
3794
3796
3795
3797
###Install node modules
3796
3798
@@ -4567,7 +4569,7 @@ var ZombieView = Backbone.View.extend({
4567
4569
4568
4570
initialize: function() {
4569
4571
// bind the model change to re-render this view
4570
-
this.model.on('change', this.render, this);
4572
+
this.listenTo(this.model, 'change', this.render);
4571
4573
},
4572
4574
4573
4575
close: function() {
@@ -5841,7 +5843,7 @@ In most cases, the view removal should not affect any associated models. For exa
5841
5843
5842
5844
(Thanks to [dira](http://stackoverflow.com/users/906136/dira) for this tip)
5843
5845
5844
-
Note: You may also be interested in reading the about Marionette Composite Views in the Extensions part of the book.
5846
+
Note: You may also be interested in reading about the Marionette Composite Views in the Extensions part of the book.
5845
5847
5846
5848
#### Rendering View Hierarchies
5847
5849
@@ -6478,7 +6480,7 @@ The difference, then, is why these two patterns are both using events. The event
6478
6480
6479
6481
Both the event aggregator and mediator, by design, use a third-party object to facilitate things. The event aggregator itself is a third-party to the event publisher and the event subscriber. It acts as a central hub for events to pass through. The mediator is also a third party to other objects, though. So where is the difference? Why don’t we call an event aggregator a mediator? The answer largely comes down to where the application logic and workflow is coded.
6480
6482
6481
-
In the case of an event aggregator, the third party object is there only to facilitate the pass-through of events from an unknown number of sources to an unknown number of handlers. All workflow and business logic that needs to be kicked off is put directly into the the object that triggers the events and the objects that handle the events.
6483
+
In the case of an event aggregator, the third party object is there only to facilitate the pass-through of events from an unknown number of sources to an unknown number of handlers. All workflow and business logic that needs to be kicked off is put directly into the object that triggers the events and the objects that handle the events.
6482
6484
6483
6485
In the case of the mediator, though, the business logic and workflow is aggregated into the mediator itself. The mediator decides when an object should have it’s methods called and attributes updated based on factors that the mediator knows about. It encapsulates the workflow and process, coordinating multiple objects to produce the desired system behaviour. The individual objects involved in this workflow each know how to perform their own task. But it’s the mediator that tells the objects when to perform the tasks by making decisions at a higher level than the individual objects.
6484
6486
@@ -6691,7 +6693,7 @@ Addy's post on [Writing Modular JS](http://addyosmani.com/writing-modular-js/) c
6691
6693
Before using RequireJS and Backbone we will first set up a very basic RequireJS project to demonstrate how it works. The first thing to do is to [Download RequireJS](http://requirejs.org/docs/download.html#requirejs). When you load in the RequireJS script in your HTML file, you need to also tell it where your main JavaScript file is located. Typically this will be called something like "app.js", and is the main entry point for your application. You do this by adding in a `data-main` attribute to the `script` tag:
Now, RequireJS will automatically load `app.js` for you.
@@ -6902,7 +6904,7 @@ I've called this collection `Cart`, as it's a group of items. As the `Item` mode
6902
6904
Finally, let's have a look at the view for this collection. (This file is much bigger in the application, but I've taken some bits out so it's easier to examine).
Copy file name to clipboardexpand all lines: backbone-fundamentals.rtf
+10-8
Original file line number
Diff line number
Diff line change
@@ -1223,7 +1223,9 @@ Todos.reset([]);\par}
1223
1223
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 \line
1224
1224
// Define a model of type 'Beatle' with a 'job' attribute\line
1225
1225
var Beatle = Backbone.Model.extend(\{\line
1226
-
job: 'musician'\line
1226
+
defaults: \{\line
1227
+
job: 'musician'\line
1228
+
\}\line
1227
1229
\});\line
1228
1230
\line
1229
1231
// Create models for each member of the Beatles\line
@@ -2448,7 +2450,7 @@ TodoMVC.com
2448
2450
{\pard \ql \f0 \sa180 \li360 \fi-360 \bullet \tx360\tab {\f1 toggleAllComplete()}: Allows a user to mark all of the items in the todo list as completed by clicking the toggle-all checkbox.\par}
2449
2451
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\f1 initialize()}: We\u8217've bound callbacks to several additional events:\par}
2450
2452
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab We\u8217've bound a {\f1 filterOne()} callback on the Todos collection for a {\f1 change:completed} event. This listens for changes to the completed flag for any model in the collection. The affected todo is passed to the callback which triggers a custom {\f1 visible} event on the model.\par}
2451
-
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab We\u8217've bound a {\f1 filterAll()} callback for a {\f1 filter} event, which works a little similar to addOne() and addAll(). It\u8217's responsibility is to toggle which todo items are visible based on the filter currently selected in the UI (all, completed or remaining) via calls to filterOne().\par}
2453
+
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab We\u8217've bound a {\f1 filterAll()} callback for a {\f1 filter} event, which works a little similar to addOne() and addAll(). Its responsibility is to toggle which todo items are visible based on the filter currently selected in the UI (all, completed or remaining) via calls to filterOne().\par}
2452
2454
{\pard \ql \f0 \sa180 \li360 \fi-360 \bullet \tx360\tab We\u8217've used the special {\f1 all} event to bind any event triggered on the Todos collection to the view\u8217's render method (discussed below).\sa180\par}
2453
2455
{\pard \ql \f0 \sa180 \li0 \fi0 The {\f1 initialize()} method completes by fetching the previously saved todos from localStorage.\par}
2454
2456
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\f1 render()}: Several things are happening in our {\f1 render()} method:\par}
@@ -3011,7 +3013,7 @@ addBook: function( e ) \{\line
{\pard \ql \f0 \sa180 \li0 \fi0 Download and install node.js from nodejs.org. The node package manager (npm) will be installed as well.\par}
3014
-
{\pard \ql \f0 \sa180 \li0 \fi0 Downloadand install MongoDB from mongodb.org. There are detailed installation guides {\field{\*\fldinst{HYPERLINK "http://docs.mongodb.org/manual/installation/"}}{\fldrslt{\ul
3016
+
{\pard \ql \f0 \sa180 \li0 \fi0 Download, install, and run MongoDB from mongodb.org (you need Mongo to be running to store data in a Mongo database). There are detailed installation guides {\field{\*\fldinst{HYPERLINK "http://docs.mongodb.org/manual/installation/"}}{\fldrslt{\ul
{\pard \ql \f0 \sa180 \li0 \fi0 Note: You may also be interested in reading the about Marionette Composite Views in the Extensions part of the book.\par}
4587
+
{\pard \ql \f0 \sa180 \li0 \fi0 Note: You may also be interested in reading about the Marionette Composite Views in the Extensions part of the book.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 Let us say you have a Collection, where each item in the Collection could itself be a Collection. You can render each item in the Collection, and indeed can render any items which themselves are Collections. The problem you might have is how to render HTML that reflects the hierarchical nature of the data structure.\par}
@@ -5079,7 +5081,7 @@ var View2 = Backbone.View.extend(\{\line
5079
5081
{\pard \ql \f0 \sa180 \li0 \fi0 The difference, then, is why these two patterns are both using events. The event aggregator, as a pattern, is designed to deal with events. The mediator, though, only uses them because it\u8217's convenient.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 Both the event aggregator and mediator, by design, use a third-party object to facilitate things. The event aggregator itself is a third-party to the event publisher and the event subscriber. It acts as a central hub for events to pass through. The mediator is also a third party to other objects, though. So where is the difference? Why don\u8217't we call an event aggregator a mediator? The answer largely comes down to where the application logic and workflow is coded.\par}
5082
-
{\pard \ql \f0 \sa180 \li0 \fi0 In the case of an event aggregator, the third party object is there only to facilitate the pass-through of events from an unknown number of sources to an unknown number of handlers. All workflow and business logic that needs to be kicked off is put directly into the the object that triggers the events and the objects that handle the events.\par}
5084
+
{\pard \ql \f0 \sa180 \li0 \fi0 In the case of an event aggregator, the third party object is there only to facilitate the pass-through of events from an unknown number of sources to an unknown number of handlers. All workflow and business logic that needs to be kicked off is put directly into the object that triggers the events and the objects that handle the events.\par}
5083
5085
{\pard \ql \f0 \sa180 \li0 \fi0 In the case of the mediator, though, the business logic and workflow is aggregated into the mediator itself. The mediator decides when an object should have it\u8217's methods called and attributes updated based on factors that the mediator knows about. It encapsulates the workflow and process, coordinating multiple objects to produce the desired system behaviour. The individual objects involved in this workflow each know how to perform their own task. But it\u8217's the mediator that tells the objects when to perform the tasks by making decisions at a higher level than the individual objects.\par}
5084
5086
{\pard \ql \f0 \sa180 \li0 \fi0 An event aggregator facilitates a \u8220"fire and forget\u8221" model of communication. The object triggering the event doesn\u8217't care if there are any subscribers. It just fires the event and moves on. A mediator, though, might use events to make decisions, but it is definitely not \u8220"fire and forget\u8221". A mediator pays attention to a known set of input or activities so that it can facilitate and coordinate additional behavior with a known set of actors (objects).\par}
5085
5087
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs20 Relationships: When To Use Which\par}
@@ -5244,7 +5246,7 @@ Writing Modular JS
5244
5246
Download RequireJS
5245
5247
}}}
5246
5248
. When you load in the RequireJS script in your HTML file, you need to also tell it where your main JavaScript file is located. Typically this will be called something like \u8220"app.js\u8221", and is the main entry point for your application. You do this by adding in a {\f1 data-main} attribute to the {\f1 script} tag:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 In the main JavaScript file that you load with the {\f1 data-main} attribute you can configure how RequireJS loads the rest of your application. This is done by calling {\f1 require.config}, and passing in an object:\par}
@@ -5392,7 +5394,7 @@ Backbone application with RequireJS that you can find on Github
5392
5394
\});\par}
5393
5395
{\pard \ql \f0 \sa180 \li0 \fi0 I\u8217've called this collection {\f1 Cart}, as it\u8217's a group of items. As the {\f1 Item} model is the second dependency, I can bind the variable {\f1 Item} to it by declaring it as the second argument to the callback function. I can then refer to this within my collection implementation.\par}
5394
5396
{\pard \ql \f0 \sa180 \li0 \fi0 Finally, let\u8217's have a look at the view for this collection. (This file is much bigger in the application, but I\u8217've taken some bits out so it\u8217's easier to examine).\par}
0 commit comments