|
1 | | -<span id="backbone.js-and-parse.com" class="anchor"></span> |
| 1 | +<span id="backbone.js-and-Parse" class="anchor"></span> |
2 | 2 | CHAPTER 5 |
3 | 3 | --------- |
4 | | -Backbone.js and Parse.com |
| 4 | +Backbone.js and Parse |
5 | 5 | ========================= |
6 | 6 | > *Java is to JavaScript what Car is to Carpet.* |
7 | 7 | > |
8 | 8 | > — [Chris Heilmann](http://christianheilmann.com/) |
9 | 9 |
|
10 | 10 | In this chapter, we'll explore the practical aspect of leveraging |
11 | | -Parse.com for a Backbone.js app. The chapter will illustrate the |
12 | | -Backbone.js uses with Parse.com and its JavaScript SDK on the modified |
| 11 | +Parse for a Backbone.js app. The chapter will illustrate the |
| 12 | +Backbone.js uses with Parse and its JavaScript SDK on the modified |
13 | 13 | Message Board app. |
14 | 14 |
|
15 | 15 | If you've written some complex client-side applications, you might have |
@@ -21,7 +21,7 @@ event listeners, and triggers. For more information and step-by-step |
21 | 21 | examples of building Backbone.js applications from scratch, please refer |
22 | 22 | to the chapter "Intro to Backbone.js." |
23 | 23 |
|
24 | | -Message Board with Parse.com: JavaScript SDK and Backbone.js Version |
| 24 | +Message Board with Parse: JavaScript SDK and Backbone.js Version |
25 | 25 | ==================================================================== |
26 | 26 |
|
27 | 27 | <span id="OLE_LINK27" class="anchor"><span id="OLE_LINK26" |
@@ -38,9 +38,15 @@ not there were changes to the data. The Backbone.js |
38 | 38 | Model-View-Controller (MVC) framework can be used to make complex |
39 | 39 | applications more manageable and easier to maintain. |
40 | 40 |
|
| 41 | + |
| 42 | + |
| 43 | +***Figure 5-1.** Message Board powered by local Parse server.* |
| 44 | + |
| 45 | + |
| 46 | + |
41 | 47 | If you felt comfortable with the previous example, let's build upon it |
42 | 48 | with the use of the Backbone.js framework. Here we'll go step by step, |
43 | | -creating a Message Board application using Backbone.js and Parse.com |
| 49 | +creating a Message Board application using Backbone.js and Parse |
44 | 50 | JavaScript SDK. If you feel familiar enough with it, you could download |
45 | 51 | the Super Simple Backbone Starter Kit at |
46 | 52 | [github.com/azat-co/super-simple-backbone-starter-kit](http://github.com/azat-co/super-simple-backbone-starter-kit). |
@@ -53,7 +59,7 @@ The application is available at |
53 | 59 | but again you are encouraged to start from scratch and try to write your |
54 | 60 | own code using the example only as a reference. |
55 | 61 |
|
56 | | -The following shows the structure of Message Board with Parse.com, |
| 62 | +The following shows the structure of Message Board with Parse, |
57 | 63 | JavaScript SDK, and Backbone.js version: |
58 | 64 |
|
59 | 65 | /06-board-backbone-parse-sdk |
@@ -110,11 +116,11 @@ Do the same for Underscore v1.8.3 and Backbone v1.2.3: |
110 | 116 | <script src="js/underscore.js"></script> |
111 | 117 | <script src="js/backbone.js"></script> |
112 | 118 |
|
113 | | -The Parse JavaScript SDK v1.5.0 is hot-linked from Parse.com CDN. Note |
114 | | -the version number, because the older versions might not work properly |
| 119 | +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 |
| 120 | +this, because other versions might not work properly |
115 | 121 | with this example: |
116 | 122 |
|
117 | | - <script src="//www.parsecdn.com/js/parse-1.5.0.min.js"></script> |
| 123 | + <script src="js/parse-1.5.0.js"></script> |
118 | 124 |
|
119 | 125 | Twitter Bootstrap CSS inclusion: |
120 | 126 |
|
@@ -175,7 +181,7 @@ We use Require.js syntax and shim plugin for HTML templates: |
175 | 181 |
|
176 | 182 | The application router with a single index route: |
177 | 183 |
|
178 | | - var ApplicationRouter = Backbone.Router.extend({ |
| 184 | + const ApplicationRouter = Backbone.Router.extend({ |
179 | 185 | routes: { |
180 | 186 | "": "home", |
181 | 187 | "*actions": "home" |
@@ -254,7 +260,7 @@ The full code of the `app.js` file: |
254 | 260 | headerTpl, |
255 | 261 | homeTpl, |
256 | 262 | footerTpl) { |
257 | | - var ApplicationRouter = Backbone.Router.extend({ |
| 263 | + const ApplicationRouter = Backbone.Router.extend({ |
258 | 264 | routes: { |
259 | 265 | '': 'home', |
260 | 266 | '*actions': 'home' |
@@ -451,23 +457,23 @@ For your convenience, here's the full code of the `home.html` template file: |
451 | 457 |
|
452 | 458 | Now we can add the following components to: |
453 | 459 |
|
454 | | -- Parse.com collection |
| 460 | +- Parse collection |
455 | 461 |
|
456 | | -- Parse.com model |
| 462 | +- Parse model |
457 | 463 |
|
458 | 464 | - Send/add message event |
459 | 465 |
|
460 | 466 | - Getting/displaying messages functions |
461 | 467 |
|
462 | | -Backbone-compatible model object/class from Parse.com JS SDK with a |
| 468 | +Backbone-compatible model object/class from Parse JS SDK with a |
463 | 469 | mandatory `className` attribute (this is the name of the collection that |
464 | | -will appear in the Data Browser of the Parse.com web interface): |
| 470 | +will appear in the Data Browser of the Parse web interface): |
465 | 471 |
|
466 | 472 | Message = Parse.Object.extend({ |
467 | 473 | className: 'MessageBoard' |
468 | 474 | }) |
469 | 475 |
|
470 | | -Backbone-compatible collection object from Parse.com JavaScript SDK that |
| 476 | +Backbone-compatible collection object from Parse JavaScript SDK that |
471 | 477 | points to the model: |
472 | 478 |
|
473 | 479 | MessageBoard = Parse.Collection.extend ({ |
@@ -512,13 +518,13 @@ Firstly, we get the form object by its ID (`#new-message`) because it's |
512 | 518 | more effective and readable to use a stored object rather than use |
513 | 519 | jQuery selector every time. |
514 | 520 |
|
515 | | - var newMessageForm = $('#new-message') |
| 521 | + const newMessageForm = $('#new-message') |
516 | 522 |
|
517 | 523 | The next two lines will get the values of the input fields with names |
518 | 524 | `username` and `message`: |
519 | 525 |
|
520 | | - var username = newMessageForm.find('[name="username"]').val() |
521 | | - var message = newMessageForm.find('[name="message"]').val() |
| 526 | + const username = newMessageForm.find('[name="username"]').val() |
| 527 | + const message = newMessageForm.find('[name="message"]').val() |
522 | 528 |
|
523 | 529 | Once we have the values of a new message (text and author), we can |
524 | 530 | invoke the `this.collection.add`: |
@@ -549,7 +555,7 @@ something like this: |
549 | 555 | |
550 | 556 | Parse.initialize('your-parse-app-id', 'your-parse-js-sdk-key') |
551 | 557 |
|
552 | | - var ApplicationRouter = Backbone.Router.extend({ |
| 558 | + const ApplicationRouter = Backbone.Router.extend({ |
553 | 559 | routes: { |
554 | 560 | '': 'home', |
555 | 561 | '*actions': 'home' |
@@ -615,9 +621,9 @@ something like this: |
615 | 621 | }) |
616 | 622 | }, |
617 | 623 | saveMessage: function(){ |
618 | | - var newMessageForm = $('#new-message') |
619 | | - var username = newMessageForm.find('[name="username"]').val() |
620 | | - var message = newMessageForm.find('[name="message"]').val() |
| 624 | + const newMessageForm = $('#new-message') |
| 625 | + const username = newMessageForm.find('[name="username"]').val() |
| 626 | + const message = newMessageForm.find('[name="message"]').val() |
621 | 627 | this.collection.add({ |
622 | 628 | 'username': username, |
623 | 629 | 'message': message |
@@ -628,11 +634,11 @@ something like this: |
628 | 634 | } |
629 | 635 | }) |
630 | 636 |
|
631 | | - app = new ApplicationRouter () |
| 637 | + window.app = new ApplicationRouter () |
632 | 638 | Backbone.history.start() |
633 | 639 | }) |
634 | 640 |
|
635 | | -The full source code of the Backbone.js and Parse.com Message Board |
| 641 | +The full source code of the Backbone.js and Parse Message Board |
636 | 642 | application is available at |
637 | 643 | <https://github.com/azat-co/fullstack-javascript/tree/master/code/06-board-backbone-parse-sdk>. |
638 | 644 |
|
@@ -642,7 +648,7 @@ Taking Message Board Further |
642 | 648 | Once you are comfortable that your front-end application works well |
643 | 649 | locally, with or without a local HTTP server like MAMP or XAMPP, deploy |
644 | 650 | it to Windows Azure or Heroku. In-depth deployment instructions are |
645 | | -described in the "jQuery and Parse.com" chapter. |
| 651 | +described in the "jQuery and Parse" chapter. |
646 | 652 |
|
647 | 653 | In the last two examples, Message Board had very basic functionality. |
648 | 654 | You could enhance the application by adding more features. |
@@ -667,7 +673,7 @@ Additional features for **advanced** level developers: |
667 | 673 |
|
668 | 674 | - Prevent the same user from voting multiple times. |
669 | 675 |
|
670 | | -- Add user sign-up and log-in actions by using Parse.com functions. |
| 676 | +- Add user sign-up and log-in actions by using Parse functions. |
671 | 677 |
|
672 | 678 | - Add a *Delete Message* button next to each message created by |
673 | 679 | a user. |
@@ -678,14 +684,14 @@ Summary |
678 | 684 | ======= |
679 | 685 |
|
680 | 686 | This short chapter gives you yet another way of building apps with |
681 | | -nothing but JavaScript (and HTML and CSS, obviously). With Parse.com or |
| 687 | +nothing but JavaScript (and HTML and CSS, obviously). With Parse or |
682 | 688 | a similar back-end-as-a-service (BaaS) solution, it is straightforward |
683 | 689 | to persist the data without having to code your own back end. BaaS |
684 | 690 | solutions event takes it a step further by allowing for access-level |
685 | 691 | controls, authentications, server-side logic, and third-party |
686 | 692 | integrations. |
687 | 693 |
|
688 | | -In addition to Parse.com, in this chapter we saw how Backbone can be |
| 694 | +In addition to Parse, in this chapter we saw how Backbone can be |
689 | 695 | flexible in terms that you can overload its classes to build your own |
690 | 696 | custom ones. This is a way to use Backbone to build your own framework. |
691 | 697 | This is what we did at DocuSign where we had base Backbone models and |
|