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
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:
2929
2929
@@ -2951,7 +2951,7 @@ body {
2951
2951
2952
2952
Now it looks a bit better:
2953
2953
2954
-

2954
+

2955
2955
2956
2956
2957
2957
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
3088
3088
3089
3089
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:
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();
3169
3169
3170
3170
If you view index.html in a browser you should see something like this:
3171
3171
3172
-

3172
+

3173
3173
3174
3174
Here is the final app.js:
3175
3175
@@ -3369,7 +3369,7 @@ initialize:function () {
3369
3369
3370
3370
Now you should be ready to take the application for a spin.
3371
3371
3372
-

3372
+

3373
3373
3374
3374
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.
3375
3375
@@ -3407,7 +3407,7 @@ var Book = Backbone.Model.extend({
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:
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:
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
3768
3768
3769
3769
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.
3770
3770
3771
-

3771
+

3772
3772
3773
3773
3774
3774
Lets move on to creating a get that retrieves a single book in server.js:
@@ -4004,7 +4004,7 @@ initialize:function () {
4004
4004
4005
4005
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:
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
4071
4071
4072
4072
Reloading the page again should look quite decent:
4073
4073
4074
-

4074
+

4075
4075
4076
4076
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:
4077
4077
@@ -4158,7 +4158,7 @@ Now in the beginning of app.js bind datepicker to our releaseDate field:
4158
4158
4159
4159
You should now be able to pick a date when clicking in the releaseDate field:
4160
4160
4161
-

4161
+

4162
4162
4163
4163
Now go to the addBook function in LibraryView and update it like this:
4164
4164
@@ -4219,7 +4219,7 @@ addBook:function (e) {
4219
4219
4220
4220
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!
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:
54
54
@@ -76,7 +76,7 @@ body {
76
76
77
77
Now it looks a bit better:
78
78
79
-

79
+

80
80
81
81
82
82
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
213
213
214
214
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:
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();
294
294
295
295
If you view index.html in a browser you should see something like this:
296
296
297
-

297
+

298
298
299
299
Here is the final app.js:
300
300
@@ -494,7 +494,7 @@ initialize:function () {
494
494
495
495
Now you should be ready to take the application for a spin.
496
496
497
-

497
+

498
498
499
499
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.
500
500
@@ -532,7 +532,7 @@ var Book = Backbone.Model.extend({
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:
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:
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
893
893
894
894
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.
895
895
896
-

896
+

897
897
898
898
899
899
Lets move on to creating a get that retrieves a single book in server.js:
@@ -1129,7 +1129,7 @@ initialize:function () {
1129
1129
1130
1130
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:
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
1196
1196
1197
1197
Reloading the page again should look quite decent:
1198
1198
1199
-

1199
+

1200
1200
1201
1201
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:
1202
1202
@@ -1283,7 +1283,7 @@ Now in the beginning of app.js bind datepicker to our releaseDate field:
1283
1283
1284
1284
You should now be able to pick a date when clicking in the releaseDate field:
1285
1285
1286
-

1286
+

1287
1287
1288
1288
Now go to the addBook function in LibraryView and update it like this:
1289
1289
@@ -1344,7 +1344,7 @@ addBook:function (e) {
1344
1344
1345
1345
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!
0 commit comments