@@ -9,9 +9,11 @@ Backbone.js and Parse |
9 | 9 | > — [Chris Heilmann](http://christianheilmann.com) |
10 | 10 |
|
11 | 11 | In this chapter, we'll explore the practical aspect of leveraging |
12 | | -Parse for a Backbone.js app. The chapter will illustrate the |
13 | | -Backbone.js uses with Parse and its JavaScript SDK on the modified |
14 | | -Message Board app. |
| 12 | +Parse for a Backbone.js app. The chapter will illustrate the following: |
| 13 | + |
| 14 | +* Backbone.js and Parse usage |
| 15 | +* Modifying Message Board with Parse and JavaScript SDK |
| 16 | +* Taking Message Board further the |
15 | 17 |
|
16 | 18 | If you've written some complex client-side applications, you might have |
17 | 19 | found that it's challenging to maintain the spaghetti code of JavaScript |
@@ -20,45 +22,39 @@ way to organize your logic into a Model-View-Controller (MVC) type of |
20 | 22 | structure. It also has nice features like URL routing, REST API support, |
21 | 23 | event listeners, and triggers. For more information and step-by-step |
22 | 24 | examples of building Backbone.js applications from scratch, please refer |
23 | | -to the chapter "Intro to Backbone.js." |
| 25 | +to Chapter 4 "Intro to Backbone.js." |
24 | 26 |
|
25 | 27 | Message Board with Parse: JavaScript SDK and Backbone.js Version |
26 | 28 | ==================================================================== |
27 | 29 |
|
28 | | -<span id="OLE_LINK27" class="anchor"><span id="OLE_LINK26" |
29 | | -class="anchor"><span id="OLE_LINK36" |
30 | | -class="anchor"></span></span></span>Supplemental video which walks you |
31 | | -through the implementation and demonstrates the project: |
32 | | -http://bit.ly/1QnqsQC. |
33 | | - |
34 | | -It's easy to see that if we keep adding more and more buttons such as |
35 | | -"DELETE," "UPDATE," and other functionalities, our system of |
36 | | -asynchronous callback will grow more complicated. And we'll have to know |
| 30 | +Speaking of Message Board with jQuery that I covered earlier, it's easy to see that if we keep adding more and more buttons such as |
| 31 | +"DELETE," "UPDATE," and other functionalities, our asynchronous callbacks will grow more complicated. And we'll have to know |
37 | 32 | when to update the view (i.e., the list of messages) based on whether or |
38 | 33 | not there were changes to the data. The Backbone.js |
39 | 34 | Model-View-Controller (MVC) framework can be used to make complex |
40 | 35 | applications more manageable and easier to maintain. |
41 | 36 |
|
| 37 | +If you felt comfortable with the previous Message Board with jQuery example, let's build upon it |
| 38 | +with the use of the Backbone.js framework. We will change the app to use Backbone but the look will remain moslty the same (see Figure 5-1). |
| 39 | + |
42 | 40 |  |
43 | 41 |
|
44 | 42 | ***Figure 5-1.** Message Board powered by local Parse server.* |
45 | 43 |
|
46 | | -If you felt comfortable with the previous example, let's build upon it |
47 | | -with the use of the Backbone.js framework. Here we'll go step by step, |
48 | | -creating a Message Board application using Backbone.js and Parse |
| 44 | +Next we'll go step by step, creating a Message Board application using Backbone.js and Parse |
49 | 45 | JavaScript SDK. If you feel familiar enough with it, you could download |
50 | 46 | the Super Simple Backbone Starter Kit at |
51 | | -[github.com/azat-co/super-simple-backbone-starter-kit](http://github.com/azat-co/super-simple-backbone-starter-kit). |
| 47 | +<https://github.com/azat-co/super-simple-backbone-starter-kit>. |
52 | 48 | Integration with Backbone.js will allow for a straightforward |
53 | 49 | implementation of user actions by binding them to asynchronous updates |
54 | 50 | of the collection. |
55 | 51 |
|
56 | 52 | The application is available at |
57 | | -<https://github.com/azat-co/fullstack-javascript/tree/master/code/06-board-backbone-parse-sdk>, |
| 53 | +<https://github.com/azat-co/fullstack-javascript/tree/master/code/05-board-backbone-parse-sdk>, TK |
58 | 54 | but again you are encouraged to start from scratch and try to write your |
59 | 55 | own code using the example only as a reference. |
60 | 56 |
|
61 | | -The following shows the structure of Message Board with Parse, |
| 57 | +The following shows the structure of the Message Board with Parse, |
62 | 58 | JavaScript SDK, and Backbone.js version: |
63 | 59 |
|
64 | 60 | /06-board-backbone-parse-sdk |
@@ -91,9 +87,9 @@ following content skeleton: |
91 | 87 | </body> |
92 | 88 | </html> |
93 | 89 |
|
94 | | -Download the necessary libraries or hot-link them from Google API. Now |
95 | | -include JavaScript libraries and Bootstrap style sheets into the |
96 | | -head element along with other important but not required *meta* |
| 90 | +Download the necessary libraries or hot-link them from a CDN. Next |
| 91 | +include JavaScript libraries and Bootstrap stylesheets into the |
| 92 | +`<head>` element along with other important but not required *meta* |
97 | 93 | elements. |
98 | 94 |
|
99 | 95 | <head> |
@@ -106,26 +102,26 @@ We need this for responsive behavior: |
106 | 102 | <meta name="viewport" |
107 | 103 | content="width=device-width, initial-scale=1.0" /> |
108 | 104 |
|
109 | | -Link jQuery v2.1.4 from a local file: |
| 105 | +Link jQuery from a local file (v2.1.4 or higher): |
110 | 106 |
|
111 | 107 | <script src="js/jquery.js"></script> |
112 | 108 |
|
113 | | -Do the same for Underscore v1.8.3 and Backbone v1.2.3: |
| 109 | +Do the same for Underscore (v1.8.3 or higher) and Backbone (v1.2.3 or higher): |
114 | 110 |
|
115 | 111 | <script src="js/underscore.js"></script> |
116 | 112 | <script src="js/backbone.js"></script> |
117 | 113 |
|
118 | | -The Parse JavaScript SDK v1.5.0 is patched meaning its modifide to work with the local Parse serve. In this version, I commented or delete the `/1` in the URL path. The file is in the js folder of this project. Note |
| 114 | +The Parse JavaScript SDK v1.5.0 is patched, meaning it's modified to work with the local Parse server. In this version, I commented or deleted the `/1` in the URL path. The file is in the `js` folder of this project. Note |
119 | 115 | this, because other versions might not work properly |
120 | 116 | with this example: |
121 | 117 |
|
122 | 118 | <script src="js/parse-1.5.0.js"></script> |
123 | 119 |
|
124 | | -Bootstrap CSS inclusion: |
| 120 | +The Bootstrap CSS inclusion requires the following: |
125 | 121 |
|
126 | 122 | <link type="text/css" rel="stylesheet" href="css/bootstrap.css" /> |
127 | 123 |
|
128 | | -We need to have RequireJS v2.1.22 for loading dependencies: |
| 124 | +We need to have RequireJS (v2.1.22 or higher) for loading dependencies: |
129 | 125 |
|
130 | 126 | <script type="text/javascript" src="libs/require.js"></script> |
131 | 127 |
|
@@ -134,8 +130,7 @@ And here's our JS application inclusion: |
134 | 130 | <script type="text/javascript" src="app.js"></script> |
135 | 131 | </head> |
136 | 132 |
|
137 | | -Populate the `<body>` element with Bootstrap scaffolding (more |
138 | | -about it in the "Basics" chapter): |
| 133 | +Populate the `<body>` element with Bootstrap scaffolding (introduced in Chapter 1, "Basics" ): |
139 | 134 |
|
140 | 135 | <body> |
141 | 136 | <div class="container-fluid"> |
@@ -162,13 +157,11 @@ about it in the "Basics" chapter): |
162 | 157 |
|
163 | 158 | Create an `app.js` file and put Backbone.js views inside: |
164 | 159 |
|
165 | | -- `headerView`: menu and app-common information |
166 | | - |
167 | | -- `footerView`: copyrights and contact links |
| 160 | +- `headerView`: Menu and app-common information |
| 161 | +- `footerView`: Copyrights and contact links |
| 162 | +- `homeView`: Home page content |
168 | 163 |
|
169 | | -- `homeView`: home page content |
170 | | - |
171 | | -We use Require.js syntax and shim plugin for HTML templates: |
| 164 | +We use Require.js syntax and the Shim plug-in for HTML templates: |
172 | 165 |
|
173 | 166 | require([ |
174 | 167 | 'libs/text!header.html', |
@@ -178,7 +171,7 @@ We use Require.js syntax and shim plugin for HTML templates: |
178 | 171 | homeTpl, |
179 | 172 | footerTpl) { |
180 | 173 |
|
181 | | -The application router with a single index route: |
| 174 | +The following code adds the application router with a single index route: |
182 | 175 |
|
183 | 176 | const ApplicationRouter = Backbone.Router.extend({ |
184 | 177 | routes: { |
@@ -248,7 +241,7 @@ To start an app, we create a new instance and call |
248 | 241 | Backbone.history.start() |
249 | 242 | }) |
250 | 243 |
|
251 | | -The full code of the `app.js` file: |
| 244 | +The full code of the `app.js` file is shown here: |
252 | 245 |
|
253 | 246 | require([ |
254 | 247 | 'libs/text!header.html', |
@@ -338,23 +331,20 @@ components) by assigning `row-fluid` and `col-md-12` classes: |
338 | 331 |
|
339 | 332 | This part has Underscore.js template instructions, which are just some |
340 | 333 | JS code wrapped in `<%` and `%>` marks. Right away we are checking that |
341 | | -the models variable is defined and not empty: |
| 334 | +the `models` variable is defined and not empty: |
342 | 335 |
|
343 | 336 | <% |
344 | 337 | if (typeof models != 'undefined' && models.length > 0) { |
345 | 338 |
|
346 | | -`_.each()` is an iteration function from the UnderscoreJS library |
347 | | -([underscorejs.org/#each](http://underscorejs.org/#each)), which does |
| 339 | +`_.each()` is an iterator function from the UnderscoreJS TK library |
| 340 | +(<http://underscorejs.org/#each>), which does |
348 | 341 | exactly what it sounds like—iterates through elements of an |
349 | 342 | object/array: |
350 | 343 |
|
351 | 344 | _.each(models, function (value, key, list) { %> |
352 | 345 | <tr> |
353 | 346 |
|
354 | | -Inside of the iterator function we have value that is a model. We can |
355 | | -access Backbone model's attributes with |
356 | | -`model.attributes.attributeName`. To output variables in Underscore, we |
357 | | -use `<%= NAME %>` instead of `<% CODE %>`: |
| 347 | +Inside of the iterator function we have the argument `value` that is a single model from `models`. We can access attributes of the Backbone model with `model.attributes.attributeName`. To output variables in Underscore, we use `<%= NAME %>` instead of `<% CODE %>`: |
358 | 348 |
|
359 | 349 | <td><%= value.attributes.username %></td> |
360 | 350 | <td><%= value.attributes.message %></td> |
@@ -386,7 +376,7 @@ add `<input>` elements: |
386 | 376 | <div class="col-md-12"> |
387 | 377 | <form class="well form-inline"> |
388 | 378 |
|
389 | | -The input element must have the name `username` because that's how we |
| 379 | +The `<input>` element must have the name `username` because that's how we |
390 | 380 | find this element and get the username value in the JavaScript code: |
391 | 381 |
|
392 | 382 | <input type="text" |
@@ -401,8 +391,8 @@ have the name. In this case, it's `message`: |
401 | 391 | class="input-small" |
402 | 392 | placeholder="Message Text" /> |
403 | 393 |
|
404 | | -Lastly, the send button must have the ID of `send`. This is what we use |
405 | | -in the Backbone's events property on the `HomeView` class: |
| 394 | +Lastly, the "SEND" button must have the ID of `send`. This is what we use |
| 395 | +in the `events` property of the Backbone's `HomeView` class: |
406 | 396 |
|
407 | 397 | <a id="send" class="btn btn-primary">SEND</a> |
408 | 398 | </form> |
@@ -454,32 +444,26 @@ For your convenience, here's the full code of the `home.html` template file: |
454 | 444 | </div> |
455 | 445 | </div> |
456 | 446 |
|
457 | | -Now we can add the following components to: |
| 447 | +Now we can add the following components: |
458 | 448 |
|
459 | 449 | - Parse collection |
460 | | - |
461 | 450 | - Parse model |
462 | | - |
463 | 451 | - Send/add message event |
464 | | - |
465 | 452 | - Getting/displaying messages functions |
466 | 453 |
|
467 | | -Backbone-compatible model object/class from Parse JS SDK with a |
468 | | -mandatory `className` attribute (this is the name of the collection that |
469 | | -will appear in the Data Browser of the Parse web interface): |
| 454 | +The following is a Backbone-compatible model/class `Parse.Object` from Parse JS SDK. It has a mandatory `className` attribute. This is the name of the collection. We define the model with `extend()`: |
470 | 455 |
|
471 | 456 | Message = Parse.Object.extend({ |
472 | 457 | className: 'MessageBoard' |
473 | 458 | }) |
474 | 459 |
|
475 | | -Backbone-compatible collection object from Parse JavaScript SDK that |
476 | | -points to the model: |
| 460 | +Next is the Backbone-compatible collection class/object `Parse.Collection` from Parse JavaScript SDK that points to the just created `Message` model using the `model` property: |
477 | 461 |
|
478 | 462 | MessageBoard = Parse.Collection.extend ({ |
479 | 463 | model: Message |
480 | 464 | }) |
481 | 465 |
|
482 | | -The home view needs to have the click event listener on the "SEND" button: |
| 466 | +The `HomeView` object needs to have the `click` event listener on the "SEND" button: |
483 | 467 |
|
484 | 468 | HomeView = Backbone.View.extend({ |
485 | 469 | el: '#content', |
@@ -488,7 +472,7 @@ The home view needs to have the click event listener on the "SEND" button: |
488 | 472 | 'click #send': 'saveMessage' |
489 | 473 | }, |
490 | 474 |
|
491 | | -When we create homeView, let's also create a collection and attach event |
| 475 | +When we create `HomeView`, let's also create a collection and attach event |
492 | 476 | listeners to it: |
493 | 477 |
|
494 | 478 | initialize: function() { |
@@ -508,8 +492,8 @@ listeners to it: |
508 | 492 | }) |
509 | 493 | }, |
510 | 494 |
|
511 | | -The definition of `saveMessage()` calls for the "SEND" button |
512 | | -click event: |
| 495 | +Next is the definition of `saveMessage()` calls for the "SEND" button |
| 496 | +`click` event that goes on the same `HomeView` object as a property: |
513 | 497 |
|
514 | 498 | saveMessage: function(){ |
515 | 499 |
|
@@ -526,16 +510,16 @@ The next two lines will get the values of the input fields with names |
526 | 510 | const message = newMessageForm.find('[name="message"]').val() |
527 | 511 |
|
528 | 512 | Once we have the values of a new message (text and author), we can |
529 | | -invoke the `this.collection.add`: |
| 513 | +invoke `this.collection.add()`: |
530 | 514 |
|
531 | 515 | this.collection.add({ |
532 | 516 | 'username': username, |
533 | 517 | 'message': message |
534 | 518 | }) |
535 | 519 | }, |
536 | 520 |
|
537 | | -Last, we output the collections by using `_.template` with the template |
538 | | -and then invoking it with data `this.collection`: |
| 521 | +Last, we output the collections by using `_.template` with the template from `this.template`, |
| 522 | +and then invoking it with the data coming from `this.collection`: |
539 | 523 |
|
540 | 524 | render: function() { |
541 | 525 | $(this.el).html(_.template(this.template)(this.collection)) |
@@ -637,47 +621,36 @@ something like this: |
637 | 621 | Backbone.history.start() |
638 | 622 | }) |
639 | 623 |
|
640 | | -The full source code of the Backbone.js and Parse Message Board |
| 624 | +Again, the full source code of the Backbone.js and Parse Message Board |
641 | 625 | application is available at |
642 | | -<https://github.com/azat-co/fullstack-javascript/tree/master/code/06-board-backbone-parse-sdk>. |
| 626 | +<https://github.com/azat-co/fullstack-javascript/tree/master/code/05-board-backbone-parse-sdk>. |
643 | 627 |
|
644 | 628 | Taking Message Board Further |
645 | 629 | ============================ |
646 | 630 |
|
647 | 631 | Once you are comfortable that your front-end application works well |
648 | 632 | locally, with or without a local HTTP server like MAMP or XAMPP, deploy |
649 | 633 | it to Microsoft Azure or Heroku. In-depth deployment instructions are |
650 | | -described in the "jQuery and Parse" chapter. |
| 634 | +described in Chapter 1, "Getting Data from Backend Using jQuery and Parse". |
651 | 635 |
|
652 | 636 | In the last two examples, Message Board had very basic functionality. |
653 | 637 | You could enhance the application by adding more features. |
654 | 638 |
|
655 | | -Additional features for `intermediate` level developers: |
656 | | - |
657 | | -- Sort the list of messages through the *updateAt* attribute before |
658 | | - displaying it. |
| 639 | +I created a list of additional features for `intermediate` level developers to implement as an exercise: |
659 | 640 |
|
| 641 | +- Sort the list of messages through the `updateAt` attribute before displaying it. |
660 | 642 | - Add a "Refresh" button to update the list of messages. |
661 | | - |
662 | | -- Save the username after the first message entry in a runtime memory |
663 | | - or in a session. |
664 | | - |
| 643 | +- Save the username after the first message entry in a runtime memory or in a session. |
665 | 644 | - Add an up-vote button next to each message, and store the votes. |
666 | | - |
667 | 645 | - Add a down-vote button next to each message, and store the votes. |
668 | 646 |
|
669 | | -Additional features for **advanced** level developers: |
| 647 | +Here are a few recommended additional features for *advanced* level developers: |
670 | 648 |
|
671 | 649 | - Add a User collection. |
672 | | - |
673 | 650 | - Prevent the same user from voting multiple times. |
674 | | - |
675 | 651 | - Add user sign-up and log-in actions by using Parse functions. |
676 | | - |
677 | | -- Add a *Delete Message* button next to each message created by |
678 | | - a user. |
679 | | - |
680 | | -- Add an *Edit Message* button next to each message created by a user. |
| 652 | +- Add a "Delete Message" button next to each message created by a user. |
| 653 | +- Add an "Edit Message" button next to each message created by a user. |
681 | 654 |
|
682 | 655 | Summary |
683 | 656 | ======= |
@@ -686,14 +659,14 @@ This short chapter gives you yet another way of building apps with |
686 | 659 | nothing but JavaScript (and HTML and CSS, obviously). With Parse or |
687 | 660 | a similar back-end-as-a-service (BaaS) solution, it is straightforward |
688 | 661 | to persist the data without having to code your own back end. BaaS |
689 | | -solutions event takes it a step further by allowing for access-level |
| 662 | +solutions take it a step further by allowing for access-level |
690 | 663 | controls, authentications, server-side logic, and third-party |
691 | 664 | integrations. |
692 | 665 |
|
693 | | -In addition to Parse, in this chapter we saw how Backbone can be |
694 | | -flexible in terms that you can overload its classes to build your own |
| 666 | +In addition to leveraging Parse in this chapter, we saw how Backbone can be |
| 667 | +flexible in terms of allowing you to overload its classes to build your own |
695 | 668 | custom ones. This is a way to use Backbone to build your own framework. |
696 | | -This is what we did at DocuSign where we had base Backbone models and |
| 669 | +This is what we did at DocuSign when I worked there; we had base Backbone models and |
697 | 670 | extended them for custom use cases. We even shared Backbone models |
698 | 671 | between the server and the browser, allowing for faster data loading. |
699 | 672 | Speaking of the server JavaScript, in the next chapter we'll explore how |
|