Skip to content

Commit 6b7b56f

Browse files
rename pngs to fix PDF build.
1 parent 3d4ab56 commit 6b7b56f

22 files changed

+467
-553
lines changed

backbone-fundamentals.epub

9.17 KB
Binary file not shown.

backbone-fundamentals.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -2923,7 +2923,7 @@ Create a new file index.html in the root of your project folder and enter this:
29232923
29242924
open this file in a browser and it should look something like this:
29252925
2926-
![](img/Screen-Shot-2012-03-05-at-12.57.08-AM1.png)
2926+
![](img/SS-Book-Tutorial-3.png)
29272927
29282928
Not so awesome. This is not a CSS tutorial, but we need to do some formatting. Create a file screen.css in your css folder and type this:
29292929
@@ -2951,7 +2951,7 @@ body {
29512951
29522952
Now it looks a bit better:
29532953
2954-
![](img/Screen-Shot-2012-03-05-at-1.06.15-AM.png)
2954+
![](img/SS-Book-Tutorial-1.png)
29552955
29562956
29572957
So this is what we want the final result to look like, but with more books. Go ahead and copy the bookContainer div a couple of times if you would like to see what it looks like. Now we are ready to start developing the actual application. Open up app.js and enter this:
@@ -3088,7 +3088,7 @@ So the view works like the model in that we use the extend function and pass it
30883088
30893089
Here we have created a new Book model by calling the Book constructor and passing an object with our desired properties. The model is then used when creating a BookView. Finally the bookView is rendered and inserted into our page. It should look something like this:
30903090
3091-
![](img/Screen-Shot-2012-03-05-at-10.53.34-PM.png)
3091+
![](img/SS-Book-Tutorial-2.png)
30923092
30933093
30943094
Looking good, we now have a working Backbone application. Since a library cannot contain just one book, we need to push further. Lets have a look at Collections. Collections contain collections of models. The models must be of the same kind, i.e. you cannot mix apples and oranges in the same collection. Other than that, collections are quite simple, you just tell them what kind of models they contain like this:
@@ -3169,7 +3169,7 @@ var libraryView = new LibraryView();
31693169
31703170
If you view index.html in a browser you should see something like this:
31713171
3172-
![](img/Screen-Shot-2012-03-06-at-9.58.03-AM.png)
3172+
![](img/SS-Book-Tutorial-4.png)
31733173
31743174
Here is the final app.js:
31753175
@@ -3369,7 +3369,7 @@ initialize:function () {
33693369
33703370
Now you should be ready to take the application for a spin.
33713371
3372-
![](img/Screen-Shot-2012-04-05-at-1.24.51-AM.png)
3372+
![](img/SS-Book-Tutorial-5.png)
33733373
33743374
As you may notice, if you leave a field blank, it will be blank in the created view as well. This is not what we want, we would like the default values to kick in. To do that we need to add a bit of logic. Also note that the file input for the cover image isn’t working, but that is left as an exercise to the reader.
33753375
@@ -3407,7 +3407,7 @@ var Book = Backbone.Model.extend({
34073407
34083408
Now it has better default behaviour
34093409
3410-
![](img/Screen-Shot-2012-04-05-at-11.41.59-AM1.png)
3410+
![](img/SS-Book-Tutorial-6.png)
34113411
34123412
###Removing models
34133413
@@ -3443,7 +3443,7 @@ We add some css to it for good looks. Note that I removed the margin of the exis
34433443
34443444
Looks okay!
34453445
3446-
![](img/Screen-Shot-2012-04-05-at-11.59.54-AM.png)
3446+
![](img/SS-Book-Tutorial-7.png)
34473447
34483448
Now we need to wire up the button to the logic. This works in the same way as with add. We start by creating a deleteBook function in the BookView
34493449
@@ -3643,7 +3643,7 @@ node server.js
36433643
36443644
If you open a browser on localhost:4711 you should see something like this:
36453645
3646-
![](img/Screen-Shot-2012-04-05-at-11.59.54-AM1.png)
3646+
![](img/SS-Book-Tutorial-8.png)
36473647
36483648
This is where we left off in Part 2, but we are now running on a server instead of directly from the files. Great job! We can now start defining routes (URLs) that the server should react to. This will be our REST API. Routes are defined by using app followed by one of the HTTP verbs get, put, post and delete, which corresponds to Create, Read, Update and Delete. Let us go back to server.js and define a simple route:
36493649
@@ -3656,7 +3656,7 @@ app.get('/api', function(req, res){
36563656
36573657
The get function will take the URL as first parameter and a function as second. The function will be called with request and response objects. Now you can restart node and go to our specified URL:
36583658
3659-
![](img/Screen-Shot-2012-04-23-at-10.09.35-AM.png)
3659+
![](img/SS-Book-Tutorial-9.png)
36603660
36613661
36623662
###Connect to database
@@ -3708,7 +3708,7 @@ jQuery.get("/api/books/", function (data, textStatus, jqXHR) {
37083708
37093709
…and press enter and you should get something like this:
37103710
3711-
![](img/Screen-Shot-2012-04-29-at-12.50.32-AM.png)
3711+
![](img/SS-Book-Tutorial-11.png)
37123712
37133713
37143714
Here I used jQuery to make the call to our REST API, since it was already loaded on the page. The returned array is obviously empty, since we have not put anything into the database yet. Lets go and create a POST route that enables this in server.js:
@@ -3768,7 +3768,7 @@ You should now get an array of size 1 back from our server. You may wonder about
37683768
37693769
MongoDB expects dates in UNIX time format (milliseconds from Jan 1st 1970), so we have to convert dates before posting. The object we get back however, contains a JavaScript Date object. Also note the _id attribute of the returned object.
37703770
3771-
![](img/Screen-Shot-2012-04-29-at-1.16.58-AM.png)
3771+
![](img/SS-Book-Tutorial-10.png)
37723772
37733773
37743774
Lets move on to creating a get that retrieves a single book in server.js:
@@ -4004,7 +4004,7 @@ initialize:function () {
40044004
40054005
Here I have replaced the row where we create a collection from the internal array with `this.collection.fetch()`. I have also added a listener on the reset event. We need to do this since the fetching of models is asynchronous and happens after the page is rendered. When the fetching is finished, Backbone will fire the reset event, which we listen to and re-render the view. If you reload the page now you should see all books that are stored on the server:
40064006
4007-
![](img/Screen-Shot-2012-04-30-at-12.27.32-AM.png)
4007+
![](img/SS-Book-Tutorial-14.png)
40084008
40094009
40104010
As you can see the date and keywords look a bit weird. The date delivered from the server is converted into a JavaScript Date object and when applied to the underscore template it will use the toString() function to display it. There isn’t very good support for formatting dates in JavaScript so we will use the dateFormat jQuery plugin to fix this. Go ahead and download it from [here](http://github.com/phstc/jquery-dateFormat) and put it in your js folder. Then go ahead and change index.html like this:
@@ -4071,7 +4071,7 @@ Here I iterate over the keywords array using the each function and print out eve
40714071
40724072
Reloading the page again should look quite decent:
40734073
4074-
![](img/Screen-Shot-2012-04-30-at-1.02.14-AM.png)
4074+
![](img/SS-Book-Tutorial-12.png)
40754075
40764076
Now go ahead and delete a book and then reload the page: Tadaa! the deleted book is back! Not cool, why is this? This happens because when we get the BookModels from the server they have an _id attribute (notice the underscore), but Backbone expects an id attribute (no underscore). Since no id attribute is present, Backbone sees this model as new and deleting a new model don’t need any synchronization. To fix this we could change the server response, but we are instead going to look at the parse function of Backbone.Model. The parse function lets you edit the server response before it is passed to the Model constructor. Update the Book model like this:
40774077
@@ -4158,7 +4158,7 @@ Now in the beginning of app.js bind datepicker to our releaseDate field:
41584158
41594159
You should now be able to pick a date when clicking in the releaseDate field:
41604160
4161-
![](img/Screen-Shot-2012-04-30-at-1.34.19-AM.png)
4161+
![](img/SS-Book-Tutorial-13.png)
41624162
41634163
Now go to the addBook function in LibraryView and update it like this:
41644164
@@ -4219,7 +4219,7 @@ addBook:function (e) {
42194219
42204220
Here I check if the current element is the keywords input field, in which case I split the string on each comma and then create a new array with keyword objects. In other words I assume that keywords are separated by commas, so I better write a comment on that in the form. Now you should be able to add new books with both release date and keywords!
42214221
4222-
![](img/Screen-Shot-2012-05-01-at-8.57.51-PM.png)
4222+
![](img/SS-Book-Tutorial-15.png)
42234223
42244224
### Connecting with a third party API
42254225

backbone-fundamentals.mobi

5.06 MB
Binary file not shown.

backbone-fundamentals.pdf

1.14 MB
Binary file not shown.

backbone-fundamentals.rtf

+1-1
Original file line numberDiff line numberDiff line change
@@ -6889,7 +6889,7 @@ jqXHR
68896889
\},\line
68906890
template: ...\line
68916891
\});\par}
6892-
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab $.view, $.model, $.collection\sa180\par}
6892+
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab .\u8198?{\i v}{\i i}{\i e}{\i w},\u8198?.model, $.collection\sa180\par}
68936893
{\pard \qc \f0 \sa180 \li0 \fi0 \emdash\emdash\emdash\emdash\emdash\par}
68946894
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab registry\par}
68956895
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab template helper\par}

chapters/05-exercise-2.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Create a new file index.html in the root of your project folder and enter this:
4848

4949
open this file in a browser and it should look something like this:
5050

51-
![](img/Screen-Shot-2012-03-05-at-12.57.08-AM1.png)
51+
![](img/SS-Book-Tutorial-3.png)
5252

5353
Not so awesome. This is not a CSS tutorial, but we need to do some formatting. Create a file screen.css in your css folder and type this:
5454

@@ -76,7 +76,7 @@ body {
7676

7777
Now it looks a bit better:
7878

79-
![](img/Screen-Shot-2012-03-05-at-1.06.15-AM.png)
79+
![](img/SS-Book-Tutorial-1.png)
8080

8181

8282
So this is what we want the final result to look like, but with more books. Go ahead and copy the bookContainer div a couple of times if you would like to see what it looks like. Now we are ready to start developing the actual application. Open up app.js and enter this:
@@ -213,7 +213,7 @@ So the view works like the model in that we use the extend function and pass it
213213

214214
Here we have created a new Book model by calling the Book constructor and passing an object with our desired properties. The model is then used when creating a BookView. Finally the bookView is rendered and inserted into our page. It should look something like this:
215215

216-
![](img/Screen-Shot-2012-03-05-at-10.53.34-PM.png)
216+
![](img/SS-Book-Tutorial-2.png)
217217

218218

219219
Looking good, we now have a working Backbone application. Since a library cannot contain just one book, we need to push further. Lets have a look at Collections. Collections contain collections of models. The models must be of the same kind, i.e. you cannot mix apples and oranges in the same collection. Other than that, collections are quite simple, you just tell them what kind of models they contain like this:
@@ -294,7 +294,7 @@ var libraryView = new LibraryView();
294294

295295
If you view index.html in a browser you should see something like this:
296296

297-
![](img/Screen-Shot-2012-03-06-at-9.58.03-AM.png)
297+
![](img/SS-Book-Tutorial-4.png)
298298

299299
Here is the final app.js:
300300

@@ -494,7 +494,7 @@ initialize:function () {
494494

495495
Now you should be ready to take the application for a spin.
496496

497-
![](img/Screen-Shot-2012-04-05-at-1.24.51-AM.png)
497+
![](img/SS-Book-Tutorial-5.png)
498498

499499
As you may notice, if you leave a field blank, it will be blank in the created view as well. This is not what we want, we would like the default values to kick in. To do that we need to add a bit of logic. Also note that the file input for the cover image isn’t working, but that is left as an exercise to the reader.
500500

@@ -532,7 +532,7 @@ var Book = Backbone.Model.extend({
532532

533533
Now it has better default behaviour
534534

535-
![](img/Screen-Shot-2012-04-05-at-11.41.59-AM1.png)
535+
![](img/SS-Book-Tutorial-6.png)
536536

537537
###Removing models
538538

@@ -568,7 +568,7 @@ We add some css to it for good looks. Note that I removed the margin of the exis
568568

569569
Looks okay!
570570

571-
![](img/Screen-Shot-2012-04-05-at-11.59.54-AM.png)
571+
![](img/SS-Book-Tutorial-7.png)
572572

573573
Now we need to wire up the button to the logic. This works in the same way as with add. We start by creating a deleteBook function in the BookView
574574

@@ -768,7 +768,7 @@ node server.js
768768

769769
If you open a browser on localhost:4711 you should see something like this:
770770

771-
![](img/Screen-Shot-2012-04-05-at-11.59.54-AM1.png)
771+
![](img/SS-Book-Tutorial-8.png)
772772

773773
This is where we left off in Part 2, but we are now running on a server instead of directly from the files. Great job! We can now start defining routes (URLs) that the server should react to. This will be our REST API. Routes are defined by using app followed by one of the HTTP verbs get, put, post and delete, which corresponds to Create, Read, Update and Delete. Let us go back to server.js and define a simple route:
774774

@@ -781,7 +781,7 @@ app.get('/api', function(req, res){
781781

782782
The get function will take the URL as first parameter and a function as second. The function will be called with request and response objects. Now you can restart node and go to our specified URL:
783783

784-
![](img/Screen-Shot-2012-04-23-at-10.09.35-AM.png)
784+
![](img/SS-Book-Tutorial-9.png)
785785

786786

787787
###Connect to database
@@ -833,7 +833,7 @@ jQuery.get("/api/books/", function (data, textStatus, jqXHR) {
833833

834834
…and press enter and you should get something like this:
835835

836-
![](img/Screen-Shot-2012-04-29-at-12.50.32-AM.png)
836+
![](img/SS-Book-Tutorial-11.png)
837837

838838

839839
Here I used jQuery to make the call to our REST API, since it was already loaded on the page. The returned array is obviously empty, since we have not put anything into the database yet. Lets go and create a POST route that enables this in server.js:
@@ -893,7 +893,7 @@ You should now get an array of size 1 back from our server. You may wonder about
893893

894894
MongoDB expects dates in UNIX time format (milliseconds from Jan 1st 1970), so we have to convert dates before posting. The object we get back however, contains a JavaScript Date object. Also note the _id attribute of the returned object.
895895

896-
![](img/Screen-Shot-2012-04-29-at-1.16.58-AM.png)
896+
![](img/SS-Book-Tutorial-10.png)
897897

898898

899899
Lets move on to creating a get that retrieves a single book in server.js:
@@ -1129,7 +1129,7 @@ initialize:function () {
11291129

11301130
Here I have replaced the row where we create a collection from the internal array with `this.collection.fetch()`. I have also added a listener on the reset event. We need to do this since the fetching of models is asynchronous and happens after the page is rendered. When the fetching is finished, Backbone will fire the reset event, which we listen to and re-render the view. If you reload the page now you should see all books that are stored on the server:
11311131

1132-
![](img/Screen-Shot-2012-04-30-at-12.27.32-AM.png)
1132+
![](img/SS-Book-Tutorial-14.png)
11331133

11341134

11351135
As you can see the date and keywords look a bit weird. The date delivered from the server is converted into a JavaScript Date object and when applied to the underscore template it will use the toString() function to display it. There isn’t very good support for formatting dates in JavaScript so we will use the dateFormat jQuery plugin to fix this. Go ahead and download it from [here](http://github.com/phstc/jquery-dateFormat) and put it in your js folder. Then go ahead and change index.html like this:
@@ -1196,7 +1196,7 @@ Here I iterate over the keywords array using the each function and print out eve
11961196

11971197
Reloading the page again should look quite decent:
11981198

1199-
![](img/Screen-Shot-2012-04-30-at-1.02.14-AM.png)
1199+
![](img/SS-Book-Tutorial-12.png)
12001200

12011201
Now go ahead and delete a book and then reload the page: Tadaa! the deleted book is back! Not cool, why is this? This happens because when we get the BookModels from the server they have an _id attribute (notice the underscore), but Backbone expects an id attribute (no underscore). Since no id attribute is present, Backbone sees this model as new and deleting a new model don’t need any synchronization. To fix this we could change the server response, but we are instead going to look at the parse function of Backbone.Model. The parse function lets you edit the server response before it is passed to the Model constructor. Update the Book model like this:
12021202

@@ -1283,7 +1283,7 @@ Now in the beginning of app.js bind datepicker to our releaseDate field:
12831283
12841284
You should now be able to pick a date when clicking in the releaseDate field:
12851285
1286-
![](img/Screen-Shot-2012-04-30-at-1.34.19-AM.png)
1286+
![](img/SS-Book-Tutorial-13.png)
12871287
12881288
Now go to the addBook function in LibraryView and update it like this:
12891289
@@ -1344,7 +1344,7 @@ addBook:function (e) {
13441344
13451345
Here I check if the current element is the keywords input field, in which case I split the string on each comma and then create a new array with keyword objects. In other words I assume that keywords are separated by commas, so I better write a comment on that in the form. Now you should be able to add new books with both release date and keywords!
13461346
1347-
![](img/Screen-Shot-2012-05-01-at-8.57.51-PM.png)
1347+
![](img/SS-Book-Tutorial-15.png)
13481348
13491349
### Connecting with a third party API
13501350
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)