GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add changes :violin: · SquaredR98/fullstack-javascript@1676211

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • chapter5
    • chapter5.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎chapter5/chapter5.md‎

Copy file name to clipboardExpand all lines: chapter5/chapter5.md
+60-87Lines changed: 60 additions & 87 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ Backbone.js and Parse
99
> — [Chris Heilmann](http://christianheilmann.com)
1010
1111
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
1517

1618
If you've written some complex client-side applications, you might have
1719
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
2022
structure. It also has nice features like URL routing, REST API support,
2123
event listeners, and triggers. For more information and step-by-step
2224
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."
2426

2527
Message Board with Parse: JavaScript SDK and Backbone.js Version
2628
====================================================================
2729

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
3732
when to update the view (i.e., the list of messages) based on whether or
3833
not there were changes to the data. The Backbone.js
3934
Model-View-Controller (MVC) framework can be used to make complex
4035
applications more manageable and easier to maintain.
4136

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+
4240
![](media/board-1.png)
4341

4442
***Figure 5-1.** Message Board powered by local Parse server.*
4543

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
4945
JavaScript SDK. If you feel familiar enough with it, you could download
5046
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>.
5248
Integration with Backbone.js will allow for a straightforward
5349
implementation of user actions by binding them to asynchronous updates
5450
of the collection.
5551

5652
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
5854
but again you are encouraged to start from scratch and try to write your
5955
own code using the example only as a reference.
6056

61-
The following shows the structure of Message Board with Parse,
57+
The following shows the structure of the Message Board with Parse,
6258
JavaScript SDK, and Backbone.js version:
6359

6460
/06-board-backbone-parse-sdk
@@ -91,9 +87,9 @@ following content skeleton:
9187
</body>
9288
</html>
9389

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*
9793
elements.
9894

9995
<head>
@@ -106,26 +102,26 @@ We need this for responsive behavior:
106102
<meta name="viewport"
107103
content="width=device-width, initial-scale=1.0" />
108104

109-
Link jQuery v2.1.4 from a local file:
105+
Link jQuery from a local file (v2.1.4 or higher):
110106

111107
<script src="js/jquery.js"></script>
112108

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):
114110

115111
<script src="js/underscore.js"></script>
116112
<script src="js/backbone.js"></script>
117113

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
119115
this, because other versions might not work properly
120116
with this example:
121117

122118
<script src="js/parse-1.5.0.js"></script>
123119

124-
Bootstrap CSS inclusion:
120+
The Bootstrap CSS inclusion requires the following:
125121

126122
<link type="text/css" rel="stylesheet" href="css/bootstrap.css" />
127123

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:
129125

130126
<script type="text/javascript" src="libs/require.js"></script>
131127

@@ -134,8 +130,7 @@ And here's our JS application inclusion:
134130
<script type="text/javascript" src="app.js"></script>
135131
</head>
136132

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" ):
139134

140135
<body>
141136
<div class="container-fluid">
@@ -162,13 +157,11 @@ about it in the "Basics" chapter):
162157

163158
Create an `app.js` file and put Backbone.js views inside:
164159

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
168163

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:
172165

173166
require([
174167
'libs/text!header.html',
@@ -178,7 +171,7 @@ We use Require.js syntax and shim plugin for HTML templates:
178171
homeTpl,
179172
footerTpl) {
180173

181-
The application router with a single index route:
174+
The following code adds the application router with a single index route:
182175

183176
const ApplicationRouter = Backbone.Router.extend({
184177
routes: {
@@ -248,7 +241,7 @@ To start an app, we create a new instance and call
248241
Backbone.history.start()
249242
})
250243

251-
The full code of the `app.js` file:
244+
The full code of the `app.js` file is shown here:
252245

253246
require([
254247
'libs/text!header.html',
@@ -338,23 +331,20 @@ components) by assigning `row-fluid` and `col-md-12` classes:
338331

339332
This part has Underscore.js template instructions, which are just some
340333
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:
342335

343336
<%
344337
if (typeof models != 'undefined' && models.length > 0) {
345338

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
348341
exactly what it sounds like—iterates through elements of an
349342
object/array:
350343

351344
_.each(models, function (value, key, list) { %>
352345
<tr>
353346

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 %>`:
358348

359349
<td><%= value.attributes.username %></td>
360350
<td><%= value.attributes.message %></td>
@@ -386,7 +376,7 @@ add `<input>` elements:
386376
<div class="col-md-12">
387377
<form class="well form-inline">
388378

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
390380
find this element and get the username value in the JavaScript code:
391381

392382
<input type="text"
@@ -401,8 +391,8 @@ have the name. In this case, it's `message`:
401391
class="input-small"
402392
placeholder="Message Text" />
403393

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:
406396

407397
<a id="send" class="btn btn-primary">SEND</a>
408398
</form>
@@ -454,32 +444,26 @@ For your convenience, here's the full code of the `home.html` template file:
454444
</div>
455445
</div>
456446

457-
Now we can add the following components to:
447+
Now we can add the following components:
458448

459449
- Parse collection
460-
461450
- Parse model
462-
463451
- Send/add message event
464-
465452
- Getting/displaying messages functions
466453

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()`:
470455

471456
Message = Parse.Object.extend({
472457
className: 'MessageBoard'
473458
})
474459

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:
477461

478462
MessageBoard = Parse.Collection.extend ({
479463
model: Message
480464
})
481465

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:
483467

484468
HomeView = Backbone.View.extend({
485469
el: '#content',
@@ -488,7 +472,7 @@ The home view needs to have the click event listener on the "SEND" button:
488472
'click #send': 'saveMessage'
489473
},
490474

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
492476
listeners to it:
493477

494478
initialize: function() {
@@ -508,8 +492,8 @@ listeners to it:
508492
})
509493
},
510494

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:
513497

514498
saveMessage: function(){
515499

@@ -526,16 +510,16 @@ The next two lines will get the values of the input fields with names
526510
const message = newMessageForm.find('[name="message"]').val()
527511

528512
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()`:
530514

531515
this.collection.add({
532516
'username': username,
533517
'message': message
534518
})
535519
},
536520

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`:
539523

540524
render: function() {
541525
$(this.el).html(_.template(this.template)(this.collection))
@@ -637,47 +621,36 @@ something like this:
637621
Backbone.history.start()
638622
})
639623

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
641625
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>.
643627

644628
Taking Message Board Further
645629
============================
646630

647631
Once you are comfortable that your front-end application works well
648632
locally, with or without a local HTTP server like MAMP or XAMPP, deploy
649633
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".
651635

652636
In the last two examples, Message Board had very basic functionality.
653637
You could enhance the application by adding more features.
654638

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:
659640

641+
- Sort the list of messages through the `updateAt` attribute before displaying it.
660642
- 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.
665644
- Add an up-vote button next to each message, and store the votes.
666-
667645
- Add a down-vote button next to each message, and store the votes.
668646

669-
Additional features for **advanced** level developers:
647+
Here are a few recommended additional features for *advanced* level developers:
670648

671649
- Add a User collection.
672-
673650
- Prevent the same user from voting multiple times.
674-
675651
- 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.
681654

682655
Summary
683656
=======
@@ -686,14 +659,14 @@ This short chapter gives you yet another way of building apps with
686659
nothing but JavaScript (and HTML and CSS, obviously). With Parse or
687660
a similar back-end-as-a-service (BaaS) solution, it is straightforward
688661
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
690663
controls, authentications, server-side logic, and third-party
691664
integrations.
692665

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
695668
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
697670
extended them for custom use cases. We even shared Backbone models
698671
between the server and the browser, allowing for faster data loading.
699672
Speaking of the server JavaScript, in the next chapter we'll explore how

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.