-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
1602 lines (1503 loc) · 77.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="author" content="" />
<link rel="author" href="humans.txt" />
<meta
name="description"
content="JavaScript tutorials and web development articles including topics like NodeJS, Angular, VueJS, Data Structures and Algorithms."
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0"
/>
<link
rel="alternative"
href="/atom.xml"
title=" Blog"
type="application/atom+xml"
/>
<title>
Backbone.js for absolute beginners - getting started (part 2: Models,
Collections and Views) | Blog
</title>
<!-- Favicons -->
<link
rel="apple-touch-icon-precomposed"
sizes="57x57"
href="/apple-touch-icon-57x57.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="114x114"
href="/apple-touch-icon-114x114.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="72x72"
href="/apple-touch-icon-72x72.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="144x144"
href="/apple-touch-icon-144x144.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="60x60"
href="/apple-touch-icon-60x60.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="120x120"
href="/apple-touch-icon-120x120.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="76x76"
href="/apple-touch-icon-76x76.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="152x152"
href="/apple-touch-icon-152x152.png"
/>
<link
rel="icon"
type="image/png"
href="/favicon-196x196.png"
sizes="196x196"
/>
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="/favicon-128.png" sizes="128x128" />
<meta name="application-name" content="'s Blog" />
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="/mstile-144x144.png" />
<meta name="msapplication-square70x70logo" content="/mstile-70x70.png" />
<meta
name="msapplication-square150x150logo"
content="/mstile-150x150.png"
/>
<meta name="msapplication-wide310x150logo" content="/mstile-310x150.png" />
<meta
name="msapplication-square310x310logo"
content="/mstile-310x310.png"
/>
<meta
name="msapplication-notification"
content="frequency=30;polling-uri=http://notifications.buildmypinnedsite.com/?feed=https://master--bgoonz-blog.netlify.app/atom.xml"
/>
<link
rel="canonical"
href="https://master--bgoonz-blog.netlify.app/backbone-js-for-absolute-beginners-getting-started-part-2/"
/>
<!-- Open Graph -->
<meta
property="og:url"
content="https://master--bgoonz-blog.netlify.app/backbone-js-for-absolute-beginners-getting-started-part-2/"
/>
<meta property="og:site_name" content=" Blog" />
<meta property="og:type" content="article" />
<meta
property="og:description"
content="This tutorial builds on top of the first part and continue with BacboneJS’s Models, Collections and Views."
/>
<meta
property="og:image"
content="https://master--bgoonz-blog.netlify.app/images/BackbonesforBeginners_large.png"
/>
<meta
property="og:title"
content="Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views)"
/>
<meta property="fb:app_id" content="761831487293468" />
<meta property="fb:admins" content="895685163" />
<!-- /Open Graph -->
<!-- Critical Styles -->
<style media="screen">
.overlay {
display: none;
}
</style>
<link rel="stylesheet" href="/stylesheets/main.css" />
<!-- loadDeferredStyles -->
<noscript id="deferred-styles">
<link rel="stylesheet" href="/stylesheets/font-awesome.min.css" />
</noscript>
<script>
var loadDeferredStyles = function () {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
document.body.appendChild(replacement);
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf =
requestAnimationFrame ||
mozRequestAnimationFrame ||
webkitRequestAnimationFrame ||
msRequestAnimationFrame;
if (raf)
raf(function () {
window.setTimeout(loadDeferredStyles, 0);
});
else window.addEventListener("load", loadDeferredStyles);
</script>
<meta name="generator" content="Hexo 5.0.2" />
</head>
<body>
<header class="titlebar mdl-shadow--2dp">
<div class="container align-center flexbox--space-between">
<!-- <div class="row"> -->
<!-- <div class="col-md-12"> -->
<span class="hidden-mobile-up">
<i class="fa fa-bars" aria-hidden="true"></i>
</span>
<a href="/" class="align-center m-y-2">
<img
src="/images/adrianmejia-logo.png"
alt=" Logo"
class="animation--bounce-in"
/>
<h1 class="hidden-mobile-down hidden"></h1>
</a>
<nav class="hidden-mobile-down navbar">
<a
href="/#home"
class="animation--radial-out"
id="Home"
onclick="track('/#home', 'menu', true)"
>Home</a
>
<a
href="/#trending-posts"
class="animation--radial-out"
id="Trending"
onclick="track('/#trending-posts', 'menu', true)"
>Trending</a
>
<a
href="/#popular-posts"
class="animation--radial-out"
id="Popular"
onclick="track('/#popular-posts', 'menu', true)"
>Popular</a
>
<a
href="/#about"
class="animation--radial-out"
id="About"
onclick="track('/#about', 'menu', true)"
>About</a
>
<a
href="/blog/"
class="animation--radial-out"
id="Blog"
onclick="track('/blog/', 'menu', true)"
>Blog</a
>
</nav>
<a
href="#"
id="search-modal"
onclick="track('#search', 'search', true)"
class="open-overlay"
>
<i class="fa fa-search" aria-hidden="true"></i>
</a>
<div id="search-overlay" class="overlay" aria-hidden="true">
<script id="movie" type="text/x-handlebars-template">
<article class="movie">
{{#photos.0}}
<a href="/{{path}}">
<img class="movie-image" src="{{photos.0}}" />
</a>
{{/photos.0}}
<div class="movie-meta">
<div class="movie-title">
<a href="/{{path}}">
{{{_highlightResult.title.value}}}
</a>
<span class="movie-year">
{{updatedYear}}
</span>
</div>
<div class="movie-rating">
{{#stars}}
<span class="ais-star-rating--star{{^.}}__empty{{/.}}">
</span>
{{/stars}}
</div>
<p>
{{{_highlightResult.excerptStrip.value}}}
</p>
<div class="movie-genres">
{{#tags}}
<div class="movie-genre">
{{.}}
</div>
{{/tags}}
</div>
</div>
</article>
</script>
<div class="search-container">
<div class="top">
<div class="search-button">
<i class="fa fa-search"></i>
</div>
<div class="input-container">
<input type="text" id="search-box" />
<div id="stats"></div>
</div>
<a href="#" class="close-overlay">
<i class="fa fa-times"></i>
</a>
</div>
<div class="content">
<div class="facets">
<div class="facet">
<div id="clear-all" class="facet-title"></div>
</div>
<div class="facet">
<div class="facet-title">
<i class="fa fa-tags" aria-hidden="true"></i> Tags
</div>
<div id="tags"></div>
</div>
<div class="facet">
<div class="facet-title">
<i class="fa fa-folder-o" aria-hidden="true"></i>
Categories
</div>
<div id="categories"></div>
</div>
<div class="facet">
<div class="facet-title">
<i class="fa fa-eye" aria-hidden="true"></i>
Views
</div>
<div id="ratings"></div>
</div>
<div class="facet">
<div class="facet-title">
<i class="fa fa-calendar" aria-hidden="true"></i>
Year
</div>
<div id="year"></div>
</div>
</div>
<div class="canvas">
<div id="hits"></div>
<div id="pagination"></div>
<div>
Powered by
<img
src="/images/Algolia_logo_bg-white.svg"
height="30"
alt="Algolia search"
/>
</div>
</div>
</div>
</div>
</div>
<script src="/js/search.babel.js"></script>
</div>
</header>
<nav class="hidden-mobile-up mdl-shadow--2dp">
<div class="container flexbox--space-between navbar">
<a href="/#home" class="animation--underline-from-center">Home</a>
<a href="/#trending-posts" class="animation--underline-from-center"
>Trending</a
>
<a href="/#popular-posts" class="animation--underline-from-center"
>Popular</a
>
<a href="/#about" class="animation--underline-from-center">About</a>
<a href="/blog/" class="animation--underline-from-center">Blog</a>
</div>
</nav>
<main>
<!--https://developers.google.com/search/docs/data-types/articles-->
<!--http://schema.org/BlogPosting-->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": ""
},
"headline": "Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views)",
"image": {
"@type": "ImageObject",
"url": "https://master--bgoonz-blog.netlify.app/images/BackbonesforBeginners_large.png",
"height": 360,
"width": 728
},
"datePublished": "2012-09-13T00:08:00-04:00",
"dateModified": "2012-09-13T00:08:00-04:00",
"author": {
"@type": "Person",
"name": ""
},
"publisher": {
"@type": "Organization",
"name": "",
"logo": {
"@type": "ImageObject",
"url": "https://master--bgoonz-blog.netlify.app/images/logo.png",
"width": 347,
"height": 50
}
},
"description": "This tutorial builds on top of the first part and continue with BacboneJS’s Models, Collections and Views."
}
</script>
<article class="container post">
<section class="hero">
<div class="muted">
<a class="category-link" href="/categories/coding/">Coding</a> >
<a class="category-link" href="/categories/coding/web-development/"
>Web Development</a
>
>
<a
class="category-link"
href="/categories/coding/web-development/backbone/"
>Backbone</a
>
</div>
<h1>
Backbone.js for absolute beginners - getting started (part 2:
Models, Collections and Views)
</h1>
<div class="muted">
<span title="Last time this post was updated">
<i class="fa fa-calendar" aria-hidden="true"></i>
Last updated
<time datetime="2012-09-13T04:08:00.000Z" itemprop="dateUpdated"
>September 13th 2012</time
>
</span>
<span class="m-x-2" title="Pageviews">
<i class="fa fa-eye" aria-hidden="true"></i>
204.8k
</span>
<span class="m-x-2" title="Click to go to the comments section">
<a href="#disqus_thread">
<i class="fa fa-comment-o" aria-hidden="true"></i>
<span
class="disqus-comment-count"
data-disqus-url="https://master--bgoonz-blog.netlify.app/backbone-js-for-absolute-beginners-getting-started-part-2/"
>0</span
>
</a>
</span>
<span>
<ul class="tag-list" itemprop="keywords">
<li class="tag-list-item">
<a class="tag-list-link" href="/tags/backbonejs/" rel="tag"
>backbonejs</a
><span class="tag-list-count">4</span>
</li>
<li class="tag-list-item">
<a class="tag-list-link" href="/tags/todo-app/" rel="tag"
>todo app</a
><span class="tag-list-count">5</span>
</li>
<li class="tag-list-item">
<a
class="tag-list-link"
href="/tags/tutorial-backbonejs/"
rel="tag"
>tutorial_backbonejs</a
><span class="tag-list-count">4</span>
</li>
</ul>
</span>
</div>
</section>
<div class="art-container" style="background-color: ">
<picture>
<source
media="(min-width: 728px)"
srcset="/images/BackbonesforBeginners_large.png"
/>
<source srcset="/images/Backbone_for_beginners_part2_small.png" />
<img
src="/images/BackbonesforBeginners_large.png"
alt="Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views)"
/>
</picture>
</div>
<div class="row">
<section class="col-sm-8 p-x-2">
<div class="post-content toc-content">
<p>
This tutorial builds on top of the
<a
href="/backbone-dot-js-for-absolute-beginners-getting-started/"
>first part</a
>
and continue with BacboneJS’s Models, Collections and Views.
</p>
<a id="more"></a>
<p>BackboneJS Tutorial series:</p>
<ol>
<li>
<a
href="/backbone-dot-js-for-absolute-beginners-getting-started/"
>Backbone.js for Absolute Beginners - Getting started (Part
1: Intro)</a
>
</li>
<li>
Backbone.js for absolute beginners - getting started (part 2:
Models, Collections and Views)
<strong>👈 you are here</strong>
</li>
<li>
<a
href="/backbonejs-for-absolute-beginners-getting-started-part-3/"
>Backbone.js for absolute beginners - getting started (part
3: CRUD)</a
>
</li>
<li>
<a
href="/backbone-js-for-absolute-beginners-getting-started-part-4/"
>Backbone.js for absolute beginners - getting started (part
4: Routers)</a
>
</li>
</ol>
<h2 id="Todo-App-in-Backbone">
<a
href="#Todo-App-in-Backbone"
class="headerlink"
title="Todo App in Backbone"
></a
>Todo App in Backbone
</h2>
<p>
After completing this example app, you will have experience and
a basic understanding of Backbone modules!
</p>
<p>
(Updated: 2013-02-02, 2013-11-24) <em>Notice</em>: This tutorial
was written using Backbone v.0.9.x, now version 1.1.x or later
are out. However, all the principles explained here apply to
both.
</p>
<p><strong>Todo app Boilerplate</strong></p>
<p>
Let’s start again with the initial
<a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/439ff34409dfc01adca7f9f96efcd726295f1aac/backbone-tutorial.html"
>HTML file</a
>
used on 1.1. Now, instead of div#container let’s add the
following HTML code:
</p>
<figure class="highlight html">
<figcaption>
<span>HTML Structure</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>Full Code</a
>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">section</span> <span class="attr">id</span>=<span class="string">"todoapp"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">header</span> <span class="attr">id</span>=<span class="string">"header"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Todos<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">id</span>=<span class="string">"new-todo"</span> <span class="attr">placeholder</span>=<span class="string">"What needs to be done?"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">header</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">section</span> <span class="attr">id</span>=<span class="string">"main"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"todo-list"</span>></span><span class="tag"></<span class="name">ul</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">section</span>></span></span><br><span class="line"><span class="tag"></<span class="name">section</span>></span></span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
We will implement a Todo list, basically an unordered list (ul)
of elements with checkboxes.
</p>
<h2 id="Backbone-Model">
<a
href="#Backbone-Model"
class="headerlink"
title="Backbone.Model"
></a
>Backbone.Model
</h2>
<p>
Models are the heart of every application. It contains the
interactive data and the logic surrounding it, such as data
validation, getters and setters, default values, data
initialization, conversions, etc. For our example, we are going
to create a model called <code>Todo</code>, which will store a
string of text (title) and whether the task has been completed
or not.
</p>
<figure class="highlight js">
<figcaption>
<span>Todo Model</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>Full Code</a
>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> app = {}; <span class="comment">// create namespace for our app</span></span><br><span class="line"></span><br><span class="line">app.Todo = Backbone.Model.extend({</span><br><span class="line"> defaults: {</span><br><span class="line"> title: <span class="string">''</span>,</span><br><span class="line"> completed: <span class="literal">false</span></span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
Notice that by convention, class names are capitalized, while
instance variables and objects are not. Another important aspect
of models is that their properties are dynamic; they can be
created on the fly and don’t have any specific types associated
with them.
</p>
<p><strong>Test what you just coded!</strong></p>
<p>
After you completed the code snippet above you can open your
browser console (chrome’s console: ctrl+shift+i -or- ⌘+alt+i)
and try this out, to get familiar with the models:
</p>
<figure class="highlight js">
<figcaption>
<span>Practice in your Browser\'s console</span>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
</td>
<td class="code">
<pre><span class="line"><span class="keyword">var</span> todo = <span class="keyword">new</span> app.Todo({<span class="attr">title</span>: <span class="string">'Learn Backbone.js'</span>, <span class="attr">completed</span>: <span class="literal">false</span>}); <span class="comment">// create object with the attributes specified.</span></span><br><span class="line">todo.get(<span class="string">'title'</span>); <span class="comment">// "Learn Backbone.js"</span></span><br><span class="line">todo.get(<span class="string">'completed'</span>); <span class="comment">// false</span></span><br><span class="line">todo.get(<span class="string">'created_at'</span>); <span class="comment">// undefined</span></span><br><span class="line">todo.set(<span class="string">'created_at'</span>, <span class="built_in">Date</span>());</span><br><span class="line">todo.get(<span class="string">'created_at'</span>); <span class="comment">// "Wed Sep 12 2012 12:51:17 GMT-0400 (EDT)"</span></span><br></pre>
</td>
</tr>
</table>
</figure>
<h2 id="Backbone-Collection">
<a
href="#Backbone-Collection"
class="headerlink"
title="Backbone.Collection"
></a
>Backbone.Collection
</h2>
<p>
As its name indicates, collections are ordered sets of models.
You can get and set models in the collection, listen for events
when any element in the array changes, and fetch for model’s
data from the server.
</p>
<p>E.g.: <code>todoList.fetch();</code></p>
<p>
Collections allow to save data (in database, file, memory), and
it requires a reference to it. Therefore, you need to specify
the <code>url</code> parameter with a relative URL, where the
model’s resource would be located on the server. Otherwise, you
will get errors like:
</p>
<p>
<code
>A "url" property or function must be
specified</code
>
</p>
<p>
We are not going to use a backend server for simplicity (I will
do a new post for that); instead, we will use HTML5’s local
storage for persistence through a Backbone’s plugin. So, we need
to define the localStorage property instead of the URL. You need
to include the backbone-localstorage.js with the rest of your
libs as
<a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>shown in the full code</a
>:
</p>
<p>
<code
><script
src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js"
type="text/javascript"></code
>
</p>
<figure class="highlight js">
<figcaption>
<span>Todo list Collection</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>Full Code</a
>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line">app.TodoList = Backbone.Collection.extend({</span><br><span class="line"> model: app.Todo,</span><br><span class="line"> <span class="built_in">localStorage</span>: <span class="keyword">new</span> Store(<span class="string">"backbone-todo"</span>)</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="comment">// instance of the Collection</span></span><br><span class="line">app.todoList = <span class="keyword">new</span> app.TodoList();</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p><strong>Test what you just coded!</strong></p>
<p>
(Google’s Chrome console shortcuts: ctrl+shift+i -or- ⌘+alt+i)
</p>
<figure class="highlight js">
<figcaption>
<span>Practice in your Browser\'s console</span>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
</td>
<td class="code">
<pre><span class="line"><span class="keyword">var</span> todoList = <span class="keyword">new</span> app.TodoList()</span><br><span class="line">todoList.create({<span class="attr">title</span>: <span class="string">'Learn Backbone\'s Collection'</span>}); <span class="comment">// notice: that `completed` will be set to false by default.</span></span><br><span class="line"><span class="keyword">var</span> lmodel = <span class="keyword">new</span> app.Todo({<span class="attr">title</span>: <span class="string">'Learn Models'</span>, <span class="attr">completed</span>: <span class="literal">true</span>});</span><br><span class="line">todoList.add(lmodel);</span><br><span class="line">todoList.pluck(<span class="string">'title'</span>); <span class="comment">// ["Learn Backbone's Collection", "Learn Models"]</span></span><br><span class="line">todoList.pluck(<span class="string">'completed'</span>); <span class="comment">// [false, true]</span></span><br><span class="line"><span class="built_in">JSON</span>.stringify(todoList); <span class="comment">// "[{"title":"Learn Backbone's Collection","completed":false,"id":"d9763e99-2267-75f5-62c3-9d7e40742aa6"},{"title":"Learn Models","completed":true}]"</span></span><br></pre>
</td>
</tr>
</table>
</figure>
<h2 id="Backbone-View">
<a
href="#Backbone-View"
class="headerlink"
title="Backbone.View"
></a
>Backbone.View
</h2>
<p>
As mentioned in
<a
href="/backbone-dot-js-for-absolute-beginners-getting-started/#1.2"
>1.2</a
>, Views doesn’t have the HTML markups for our application, but
instead (It’s like the controller in MVC frameworks) process
data and link it to templates. It finally render HTML based on
events or data changes.
</p>
<h3 id="Basic-Properties">
<a
href="#Basic-Properties"
class="headerlink"
title="Basic Properties"
></a
>Basic Properties
</h3>
<p>
There are four basic properties in a view: el, initialize,
render, and events.
</p>
<p>
We have already seen the first three and will see later the
fourth one. Do you remember the Hello World View from
<a
href="/backbone-dot-js-for-absolute-beginners-getting-started/#1.2"
>1.2</a
>?
</p>
<figure class="highlight js">
<figcaption><span>Example of a Backbone.View</span></figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre>
</td>
<td class="code">
<pre><span class="line"><span class="keyword">var</span> AppView = Backbone.View.extend({</span><br><span class="line"> <span class="comment">// el - stands for element. Every view has a element associate in with HTML</span></span><br><span class="line"> <span class="comment">// content will be rendered.</span></span><br><span class="line"> el: <span class="string">'#container'</span>,</span><br><span class="line"> <span class="comment">// It's the first function called when this view it's instantiated.</span></span><br><span class="line"> initialize: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.render();</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">// $el - it's a cached jQuery object (el), in which you can use jQuery functions</span></span><br><span class="line"> <span class="comment">// to push content. Like the Hello World in this case.</span></span><br><span class="line"> render: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.$el.html(<span class="string">"Hello World"</span>);</span><br><span class="line"> }</span><br><span class="line">});</span><br></pre>
</td>
</tr>
</table>
</figure>
<h3 id="view-el">
<a href="#view-el" class="headerlink" title="view.el"></a
><code>view.el</code>
</h3>
<p>
Every view needs to reference a DOM at all times. Therefore, the
view will inject content into this element. This is the
<code>el</code> property. <code>this.el</code> is created from
view’s <code>el</code>,<code>tagName</code>,
<code>className</code>, <code>id</code> or
<code>attributes</code> properties. If none of these are
specified, then this.el is an empty <code>div</code>. The
<code>view.$el</code> it’s a cached jQuery object of the view’s
element (view.el).
</p>
<h3 id="Initialize-constructor">
<a
href="#Initialize-constructor"
class="headerlink"
title="Initialize/constructor"
></a
>Initialize/constructor
</h3>
<p>
Here you can pass parameters that will be attached to a model,
collection, or view.el.
</p>
<h3 id="render">
<a href="#render" class="headerlink" title="render"></a
><code>render</code>
</h3>
<p>
This function injects the markup into the elements. Not all
views require having a render function. As you are going to see
in the sample code, they can call other view’s render functions.
</p>
<h3 id="delegated-events">
<a
href="#delegated-events"
class="headerlink"
title="delegated events"
></a
>delegated events
</h3>
<p>Events are written in the following format:</p>
<p>
<code
>{"<EVENT_TYPE> <ELEMENT_ID>":
"<CALLBACK_FUNTION>"}</code
>
</p>
<p>E.g.</p>
<p>
<code
>events: {'keypress #new-todo':
'createTodoOnEnter'}</code
>
</p>
<p>in jQuery it would be something like:</p>
<p>
<code>$('#new-todo').keypress(createTodoOnEnter);</code>
</p>
<h2 id="Todo-View">
<a href="#Todo-View" class="headerlink" title="Todo View"></a
>Todo View
</h2>
<p>
Now back to our Todo application: We need a view that renders
each of the todo model objects into the page. The
<code>item-template</code> and <code>app.TodoView</code> will
generate each todo item.
</p>
<figure class="highlight html">
<figcaption>
<span>item-template</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>Full Code</a
>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/template"</span> <span class="attr">id</span>=<span class="string">"item-template"</span>></span></span><br><span class="line"><span class="javascript"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"view"</span>></span></span><br><span class="line"><span class="javascript"> <input <span class="class"><span class="keyword">class</span></span>=<span class="string">"toggle"</span> type=<span class="string">"checkbox"</span>></span></span><br><span class="line"><span class="handlebars"><span class="xml"> <span class="tag"><<span class="name">label</span>></span><span class="tag"><<span class="name">%-</span> <span class="attr">title</span> %></span><span class="tag"></<span class="name">label</span>></span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml"> <span class="tag"></<span class="name">div</span>></span></span></span></span><br><span class="line"><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
In the following block of code, we have the Backbone.View which
uses the above template (<code>#item-template</code>) to fill
out the title from the <code>model</code> we pass along.
</p>
<figure class="highlight js">
<figcaption>
<span>Todo View</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/fe0efb0fd0c4c3c4cb5fd61e9917165082f9a562/backbone-tutorial.html"
>Full Code</a
>
</figcaption>
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="comment">// renders individual todo items list (li)</span></span><br><span class="line">app.TodoView = Backbone.View.extend({</span><br><span class="line"> tagName: <span class="string">'li'</span>,</span><br><span class="line"> template: _.template($(<span class="string">'#item-template'</span>).html()),</span><br><span class="line"> render: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.$el.html(<span class="built_in">this</span>.template(<span class="built_in">this</span>.model.toJSON()));</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">this</span>; <span class="comment">// enable chained calls</span></span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
When we instantiate a Backbone View, it can receive any
parameter that we need. In this case, since we call the
parameter <code>model</code> let’s instantiate it with a
Backbone Model (e.g. todo):
</p>
<p>
<code
>var view = new app.TodoView({model: todo});</code
>
</p>
<p>
Also, notice that our view uses a
<code>tagName: li</code> instead of just <code>el</code> from
before. This means that the new rendered elements will be
wrapped around a <code><li></li></code>
</p>
<h2 id="Backbone-Events">
<a
href="#Backbone-Events"
class="headerlink"
title="Backbone.Events"
></a
>Backbone.Events
</h2>
<p>
This module can be mixed with any object and give it the pub/sub
(observer pattern) behavior. Events provide a couple of methods
from which we will discuss: <code>on</code>,
<code>off</code> and <code>trigger</code>. (If this you are
familiar with, then in jQuery, they will work the same way +
some excellent built-in features)
</p>
<p>
<strong>Subscribing to Events with on</strong>
<code>object.on(event, callback, [context])</code>
</p>
<p>
Also called bind. It binds an object to an event and a callback.
When that event it’s triggered, it executes the callback.
</p>
<p>
E.g. <code>todoList.on('add', this.addAll, this);</code>
</p>
<p>
Every time a new item is <code>add</code>ed to a
Backbone.Collection the built-in event <code>add</code> (<a
target="_blank"
rel="noopener"
href="http://backbonejs.org/#Collection-add"
>docs for add</a
>
is triggered. In the example above, after the custom event is
triggered, the todoList’s callback <code>addAll()</code> is
executed. Also, the current object is passed with
<code>this</code> as a <code>context</code>.
</p>
<p>
Events can also be set on arbitrary objects using underscore.js
<code>extend</code> function:
</p>
<figure class="highlight js">
<table>
<tr>
<td class="gutter">
<pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
</td>
<td class="code">
<pre><span class="line"><span class="keyword">var</span> object = {},</span><br><span class="line"> callback = <span class="function"><span class="keyword">function</span>(<span class="params">msg</span>) </span>{ <span class="built_in">console</span>.log(<span class="string">"Triggered "</span> + msg); };</span><br><span class="line"></span><br><span class="line">_.extend(object, Backbone.Events);</span><br><span class="line"></span><br><span class="line">object.on(<span class="string">"my_event"</span>, callback);</span><br><span class="line"></span><br><span class="line">object.trigger(<span class="string">"my_event"</span>, <span class="string">"my custom event"</span>);</span><br></pre>
</td>
</tr>
</table>
</figure>
<h2 id="App-View">
<a href="#App-View" class="headerlink" title="App View"></a>App
View
</h2>
<p>
Now, we need another view that takes a collection and renders
each of the individual items. We are going to call it ‘AppView’.
This is a new large chunk of code so read it closely. Please
take a look through this code and identify each of the elements.
</p>