-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
1281 lines (1187 loc) · 53.7 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 4: Routers) |
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-4/"
/>
<!-- Open Graph -->
<meta
property="og:url"
content="https://master--bgoonz-blog.netlify.app/backbone-js-for-absolute-beginners-getting-started-part-4/"
/>
<meta property="og:site_name" content=" Blog" />
<meta property="og:type" content="article" />
<meta
property="og:description"
content="This tutorial is about BackboneJS Routes."
/>
<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 4: Routers)"
/>
<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 4: Routers)",
"image": {
"@type": "ImageObject",
"url": "https://master--bgoonz-blog.netlify.app/images/BackbonesforBeginners_large.png",
"height": 360,
"width": 728
},
"datePublished": "2012-09-13T14:41:00-04:00",
"dateModified": "2012-09-13T14:41: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 is about BackboneJS Routes."
}
</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 4:
Routers)
</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-13T18:41: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>
71.6k
</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-4/"
>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_part4_small.png" />
<img
src="/images/BackbonesforBeginners_large.png"
alt="Backbone.js for absolute beginners - getting started (part 4: Routers)"
/>
</picture>
</div>
<div class="row">
<section class="col-sm-8 p-x-2">
<div class="post-content toc-content">
<p>This tutorial is about BackboneJS Routes.</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>
<a
href="/backbone-js-for-absolute-beginners-getting-started-part-2/"
>Backbone.js for absolute beginners - getting started (part
2: Models, Collections and Views)</a
>
</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>
Backbone.js for absolute beginners - getting started (part 4:
Routers) <strong>👈 you are here</strong>
</li>
</ol>
<h2 id="Backbone-Router">
<a
href="#Backbone-Router"
class="headerlink"
title="Backbone.Router"
></a
>Backbone.Router
</h2>
<p>
You could build web application without using the routers.
However, if you want to make reference to certain ‘state’ or
location of the web application, you need a reference (link/URL)
to it. This is where routers come to rescue.
</p>
<p>
Routing in most of JS application are achieved by hash-tags.
E.g. If you take a look of Gmail URL you will see something
like:
</p>
<p>
<code>
https://mail.google.com/mail/u/0/#inbox/139c0d48e11d986b</code
>
</p>
<p>
where the <code>#inbox/139c0d48e11d986b </code> is the hash-tag
which reference some email location.
</p>
<p>
In backbone, routes are hash maps that match URL patterns to
functions. You can use parameter parts, such as
<code>todos/:id</code>, or using splats
<code>file/*path</code> you will match all the parameters from
the splat on. For that reason, the splat parameter should be
always the last matcher.
</p>
<h3 id="Initializing-the-Router">
<a
href="#Initializing-the-Router"
class="headerlink"
title="Initializing the Router"
></a
>Initializing the Router
</h3>
<p>
In our Todo app, we are going to use routers to filter between
the tasks that are pending and the ones that have been
completed. So, let’s initialize the routes this way:
</p>
<figure class="highlight js">
<figcaption>
<span>Define Router</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/327ac4fc4657e73fdf7157e230b1ed7cd1519667/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><span class="line">12</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line">app.Router = Backbone.Router.extend({</span><br><span class="line"> routes: {</span><br><span class="line"> <span class="string">'*filter'</span> : <span class="string">'setFilter'</span></span><br><span class="line"> },</span><br><span class="line"> setFilter: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'app.router.params = '</span> + params); <span class="comment">// just for didactical purposes.</span></span><br><span class="line"> <span class="built_in">window</span>.filter = params.trim() || <span class="string">''</span>;</span><br><span class="line"> app.todoList.trigger(<span class="string">'reset'</span>);</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>Now, you need to initialize it, adding this lines:</p>
<figure class="highlight diff">
<figcaption>
<span>Initialize router</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/327ac4fc4657e73fdf7157e230b1ed7cd1519667/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"> //--------------</span><br><span class="line"> // Initializers</span><br><span class="line"> //--------------</span><br><span class="line"></span><br><span class="line"><span class="addition">+ app.router = new app.Router();</span></span><br><span class="line"><span class="addition">+ Backbone.history.start();</span></span><br><span class="line"> app.appView = new app.AppView();</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
You can test that you router is working just typing
<code>#anything/that/you/want</code> and seeing the parameter in
you browser’s console.
</p>
<h3 id="2-6-1-Processing-the-routes">
<a
href="#2-6-1-Processing-the-routes"
class="headerlink"
title="2.6.1 Processing the routes"
></a
>2.6.1 Processing the routes
</h3>
<p>
Before rendering the list of items, you need to check the
parameters to wether show only the pending ones, or the
completed or show them all. As shown in the code snipet below.
</p>
<figure class="highlight diff">
<figcaption>
<span>Processing the routes in app.AppView</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/327ac4fc4657e73fdf7157e230b1ed7cd1519667/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><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="meta">@@ -164,7 +177,18 @@</span></span><br><span class="line"> },</span><br><span class="line"> addAll: function(){</span><br><span class="line"> this.$('#todo-list').html(''); // clean the todo list</span><br><span class="line"><span class="deletion">- app.todoList.each(this.addOne, this);</span></span><br><span class="line"><span class="addition">+ // filter todo item list</span></span><br><span class="line"><span class="addition">+ switch(window.filter){</span></span><br><span class="line"><span class="addition">+ case 'pending':</span></span><br><span class="line"><span class="addition">+ _.each(app.todoList.remaining(), this.addOne);</span></span><br><span class="line"><span class="addition">+ break;</span></span><br><span class="line"><span class="addition">+ case 'completed':</span></span><br><span class="line"><span class="addition">+ _.each(app.todoList.completed(), this.addOne);</span></span><br><span class="line"><span class="addition">+ break;</span></span><br><span class="line"><span class="addition">+ default:</span></span><br><span class="line"><span class="addition">+ app.todoList.each(this.addOne, this);</span></span><br><span class="line"><span class="addition">+ break;</span></span><br><span class="line"><span class="addition">+ }</span></span><br><span class="line"> },</span><br><span class="line"> newAttributes: function(){</span><br><span class="line"> return {</span><br><span class="line"></span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
If you try adding the words <code>#/pending</code> or
<code>#/completed</code> at the end of the URL you’ll get an
error!. That’s a good sign, it means the routes are working, but
we haven’t implemented the
<code>app.todoList.remaining()</code> and
<code>app.todoList.completed()</code>. So, that’s next:
</p>
<figure class="highlight diff">
<figcaption>
<span
>Defining completed and remaining functions in
app.TodoList</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/327ac4fc4657e73fdf7157e230b1ed7cd1519667/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><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="meta">@@ -85,7 +90,15 @@</span></span><br><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="deletion">- localStorage: new Store("backbone-todo")</span></span><br><span class="line"><span class="addition">+ localStorage: new Store("backbone-todo"),</span></span><br><span class="line"><span class="addition">+ completed: function() {</span></span><br><span class="line"><span class="addition">+ return this.filter(function( todo ) {</span></span><br><span class="line"><span class="addition">+ return todo.get('completed');</span></span><br><span class="line"><span class="addition">+ });</span></span><br><span class="line"><span class="addition">+ },</span></span><br><span class="line"><span class="addition">+ remaining: function() {</span></span><br><span class="line"><span class="addition">+ return this.without.apply( this, this.completed() );</span></span><br><span class="line"><span class="addition">+ }</span></span><br><span class="line"> });</span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
Now, if you try again adding the hash-tags it will work! But, it
will be better if the user can have links to that instead of
typing URLs. So, let’s add them.
</p>
<figure class="highlight diff">
<figcaption>
<span>Show routes' links</span
><a
target="_blank"
rel="noopener"
href="https://raw.github.com/amejiarosario/Backbone-tutorial/327ac4fc4657e73fdf7157e230b1ed7cd1519667/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><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre>
</td>
<td class="code">
<pre><span class="line"></span><br><span class="line"><span class="meta">@@ -32,6 +32,11 @@</span></span><br><span class="line"> <header id="header"></span><br><span class="line"> <h1>Todos</h1></span><br><span class="line"> <input id="new-todo" placeholder="What needs to be done?" autofocus></span><br><span class="line"><span class="addition">+ <div></span></span><br><span class="line"><span class="addition">+ <a href="#/">show all</a> |</span></span><br><span class="line"><span class="addition">+ <a href="#/pending">show pending</a> |</span></span><br><span class="line"><span class="addition">+ <a href="#/completed">show completed</a></span></span><br><span class="line"><span class="addition">+ </div></span></span><br><span class="line"> </header></span><br><span class="line"> <section id="main"></span><br><span class="line"> <ul id="todo-list"></ul></span><br><span class="line"></span><br><span class="line"></span><br></pre>
</td>
</tr>
</table>
</figure>
<p>
Well, that’s all! If completed these 4 parts tutorial you will
be familiar with the main Backbone modules (Models, Collections,
Views, Events, and Routes). To increase you knowledge you can
follow the following resources:
</p>
<ul>
<li>
<a
target="_blank"
rel="noopener"
href="https://github.com/documentcloud/backbone/blob/master/backbone.js"
>Backbone’s Source code - it’s the ultimate source of
true</a
>
</li>
<li>
<a
target="_blank"
rel="noopener"
href="http://backbonejs.org/?utm_source=adrianmejia.com"
>Official documentation</a
>
</li>
</ul>
<h2 id="What’s-next">
<a
href="#What’s-next"
class="headerlink"
title="What’s next?"
></a
>What’s next?
</h2>
<p>Write a server API in NodeJS to apply the learned here:</p>
<ul>
<li>
<a
href="/blog/2014/10/01/creating-a-restful-api-tutorial-with-nodejs-and-mongodb/"
target="_blank"
>Creating a RESTful API with NodeJS and MongoDB</a
>
</li>
</ul>
<p>Now, do a Todo app in AngularJS:</p>
<ul>
<li>
<a
href="/blog/2014/09/28/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/"
target="_blank"
>AngularJS tutorial for beginners with NodeJS ExpressJS and
MongoDB</a
>
</li>
</ul>
<p>Hope it was helpful!</p>
</div>
<!-- after post -->
<div class="hidden-mobile-down">
<h3>Now, your turn!</h3>
Thanks for reading this far. Here are some things you can do next:
<ul>
<li>
Found a typo?
<a
target="_blank"
rel="noopener"
href="https://github.com/amejiarosario/amejiarosario.github.io/edit/source/source/_posts/2012-09-13-backbone-js-for-absolute-beginners-getting-started-part-4.markdown"
>
Edit this post</a
>.
</li>
<li>
Got questions?
<a href="#comments-section">comment</a>
below.
</li>
<li>
Was it useful? Show your
<!-- <a href="/support">support</a> -->
support and share it.
<!-- <a target="_blank" href="http://paypal.me/amejiarosario">Paypal</a>, -->
<!-- <a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HZLFWQAFENN7W&source=url">Paypal</a>,
<a target="_blank" href="https://bitaps.com/3A8xanut3wddz1jJ7JWSw8nwwpUahLJpZi">Bitcoin</a>,
<a target="_blank" href="https://www.patreon.com/amejiarosario">Patreon</a>. -->
</li>
</ul>
</div>
<div class="hidden-mobile-up">
<a
target="_blank"
rel="noopener"
href="https://books.adrianmejia.com"
>
<!-- <img src="/images/data-structure-algorithms-javascript-presell.jpg" alt="Data Structure and Algorithms in JavaScript eBook"> -->
<img
src="/images/data-structure-algorithms-banner.jpg"
alt="Data Structure and Algorithms in JavaScript eBook"
/>
</a>
</div>
<!-- Next and previous posts -->
<div class="article-nav m-y-4">
<a
href="/algorithms-for-dummies-part-1-sorting/"
class="article-nav-newer"
class="article-nav-link-wrap"
>
<strong class="article-nav-caption"
><i class="fa fa-chevron-left"></i> newer</strong
>
<div class="article-nav-title">
Algorithms for dummies (Part 1): Big-O Notation and Sorting
</div>
</a>
<a
href="/backbonejs-for-absolute-beginners-getting-started-part-3/"
class="article-nav-older"
class="article-nav-link-wrap"
>
<strong class="article-nav-caption"
>older <i class="fa fa-chevron-right"></i
></strong>
<div class="article-nav-title">
Backbone.js for absolute beginners - getting started (part 3:
CRUD)
</div>
</a>
</div>
<!-- Related -->
<footer>
<!-- subscribe -->
<div class="m-y-4">
<!-- Begin MailChimp Signup Form -->
<link
href="//cdn-images.mailchimp.com/embedcode/horizontal-slim-10_7.css"
rel="stylesheet"
type="text/css"
/>
<style type="text/css">
#mc_embed_signup {
background: #fff;
clear: left;
font: 14px Helvetica, Arial, sans-serif;
width: 100%;
}
/* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<form
action="//adrianmejia.us2.list-manage.com/subscribe/post?u=2294bb28c5931a6f2338d2474&id=52678e825b"
method="post"
id="mc-embedded-subscribe-form"
name="mc-embedded-subscribe-form"
class="validate"
novalidate
>
<div id="mc_embed_signup_scroll">
<label for="mce-EMAIL"
>Subscribe & stay up to date!</label
>
<input
type="email"
value=""
name="EMAIL"
class="email"
id="mce-EMAIL"
placeholder="email address"
required
/>
<!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div
style="position: absolute; left: -5000px"
aria-hidden="true"
>
<input
type="text"
name="b_2294bb28c5931a6f2338d2474_52678e825b"
tabindex="-1"
value=""
/>
</div>
<div class="clear">
<input
type="submit"
value="Subscribe"
name="subscribe"
id="mc-embedded-subscribe"
class="button"
/>
</div>
</div>
</form>
</div>
<!--End mc_embed_signup-->
<script type="text/javascript">
var form = document.getElementById(
"mc-embedded-subscribe-form"
);
form.addEventListener("submit", function (event) {
event.preventDefault();
ga("send", "event", "newsletter", "signup", "page", {
hitCallback: createFunctionWithTimeout(function () {
form.submit();
}),
});
});
</script>
</div>
<!-- Author Bio -->
<b>About the author</b>
<!-- Bio -->
<section class="bio-box">
<img
class="m-a-1 not-scaled"
src="/images/adrian-mejia-headshot.png"
alt=""
/>
<article
class="p-x-1"
style="display: flex; flex-direction: column"
>
<p class="muted">
is a Software Engineer located in Boston, MA. Currently
working at Google. Adrian enjoys writing posts about
Algorithms, programming, JavaScript, and Web Dev. Also, he
likes to travel ✈️ and biking 🚴.
<!-- Find out more <a href="/#about">here</a>. -->
</p>
<!-- <p class="m-y-0"><a target="_blank" rel="noopener" href="https://twitter.com/iAmAdrianMejia">Follow him on Twitter</a>.</p> -->
<a
target="_blank"
rel="noopener"
href="https://twitter.com/iAmAdrianMejia"
id="twitter-follow-button"
class="twitter-follow-button"
data-show-count="false"
>Follow @iAmAdrianMejia</a
>
</article>
</section>
<div class="m-y-4">
<div class="container">
<h1 class="capitalize">tutorial backbonejs Series</h1>
<div class="row">
<!-- <ol> -->
<article class="col-md-6 col-sm-6 card">
<a
href="/backbone-dot-js-for-absolute-beginners-getting-started/"
>
<img
src="/images/Backbonesforbeginners_small.png"
width="300"
height="250"
/>
<section class="p-a-1">
<h3>
Backbone.js for Absolute Beginners - Getting started
(Part 1: Intro)
</h3>
</section>
</a>
</article>
<!-- </ol> -->
<!-- <ol> -->
<article class="col-md-6 col-sm-6 card">
<a
href="/backbone-js-for-absolute-beginners-getting-started-part-2/"
>
<img
src="/images/Backbone_for_beginners_part2_small.png"
width="300"
height="250"