-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbonejs-and-zend-framework-2-facebook-like-wall.html
1490 lines (1279 loc) · 177 KB
/
backbonejs-and-zend-framework-2-facebook-like-wall.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">
<head>
<meta charset="utf-8" />
<title>Backbone.js and Zend Framework 2 Facebook like wall</title>
<link rel="stylesheet" href="http://www.piotrdeszynski.com/theme/css/main.css" />
<link href="http://www.piotrdeszynski.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Yet another boring programmer's blog Atom Feed" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body id="index" class="home">
<a href="https://github.com/piteer1">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
</a>
<header id="banner" class="body">
<h1><a href="http://www.piotrdeszynski.com/">Yet another boring programmer's blog </a></h1>
<nav><ul>
<li><a href="http://www.piotrdeszynski.com/category/oop.html">OOP</a></li>
<li><a href="http://www.piotrdeszynski.com/category/misc.html">Misc</a></li>
<li class="active"><a href="http://www.piotrdeszynski.com/category/javascript.html">JavaScript</a></li>
<li><a href="http://www.piotrdeszynski.com/category/about.html">About</a></li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<article>
<header>
<h1 class="entry-title">
<a href="http://www.piotrdeszynski.com/backbonejs-and-zend-framework-2-facebook-like-wall.html" rel="bookmark"
title="Permalink to Backbone.js and Zend Framework 2 Facebook like wall">Backbone.js and Zend Framework 2 Facebook like wall</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2013-04-07T20:00:00">
ndz 07 kwietnia 2013
</abbr>
<address class="vcard author">
By <a class="url fn" href="http://www.piotrdeszynski.com/author/piotr-deszynski.html">Piotr Deszyński</a>
</address>
<p>In <a href="http://www.piotrdeszynski.com/category/javascript.html">JavaScript</a>. </p>
<p>tags: <a href="http://www.piotrdeszynski.com/tag/backbonejs.html">Backbone.js</a><a href="http://www.piotrdeszynski.com/tag/zend-framework-2.html">Zend Framework 2</a></p>
</footer><!-- /.post-info --> <h1>I love spaghetti code!</h1>
<p>Yup, I really love it, cause I see it everyday looking into my old code or my teammates. The worst looking is always JavaScript code. Why? I would say it's because the JavaScript is the most powerful language. It allows you to express yourself in many ways and do almost everything. Because of that it's really important to find some good conventions.</p>
<p>For long time MVC pattern is used why not to use it also in JavaScript? It will help structuring the code. So we can start writing our MVC framework... but wait! There are already plenty of these!</p>
<p>The 3 most known MVC frameworks right now are (to be honest not exactly MVC but really close):</p>
<ul>
<li><a href="http://angularjs.org/">AngularJS</a></li>
<li><a href="http://backbonejs.org/">Backbone.js</a></li>
<li><a href="http://emberjs.com/">Ember.js</a></li>
</ul>
<p>From these three I've chosen <strong>Backbone.js</strong> from simple reason. It allows to use existing DOM. Sadly the other ones are not meant for that. If your app would be an internal app or a SEO is not your priority at all then I would suggest using different framework than <strong>Backbone</strong>. But if SEO matters for you, then I would say that the best pick will be Backbone (until you would like to use for e.g. a <strong>Phantom.js</strong> to crawl your site and generate HTML files for <strong>Google</strong>).</p>
<h2>Foreword</h2>
<p>This application is an 'not finished', but working example. It would need some refactoring before sending it to 'production'. I had a limited time while playing with Backbone and that's all what I was able to do. If you're interested in this tutorial, please leave a comment and I will try to refactor this code.</p>
<h2>Let's start with an app!</h2>
<p>As an example app I've decided to implement simple <strong>FB</strong> like wall with likes mechanism. I didn't want to generate everything from JavaScript but preload data earlier. Server side part I've decided to write in PHP using <strong>Zend Framework 2</strong> (as a big fan of <strong>ZF1</strong>).</p>
<p>One of the important things was not writing any views twice. It's really often when you use JavaScript and PHP to write some views twice, once inside PHP as a returned response, secondary inside JS itself when some additional data is loaded through AJAX. It's really bad if you will catch yourself doing that. It leads to a lot of code duplication and need of change in many files when altering HTML structure. This can cause a lot of hard to find bugs, when pre-loaded page will work ok, but all dynamically loaded elements will cause you troubles. Let's say no to that!</p>
<h3>ZF2 Skeleton App</h3>
<p>As many <strong>ZF2</strong> apps it will start with getting ZF2 Skeleton app from github. So firstly create a <code>backbonejs</code> folder inside your webserver root and then create the app using composer.phar</p>
<div class="highlight"><pre>curl -s https://getcomposer.org/installer | php --
php composer.phar create-project --repository-url<span class="o">=</span><span class="s2">"http://packages.zendframework.com"</span> zendframework/skeleton-application backbonejs
</pre></div>
<p>Alternatively (like me) instead of the root folder inside webserver you can use your workspace and <a href="http://www.vagrantup.com/">Vagrant</a>. I live it to your choice.</p>
<p>It's also necessary to setup webserver correctly to run <strong>ZF2</strong>, for that please refer to <a href="http://framework.zend.com/manual/2.1/en/user-guide/overview.html">instruction</a> available on ZF2 documentation page.</p>
<p>I'm assuming that you were able to start the ZF2 application correctly and it's up and running.</p>
<p>Normally before you would be able to share something, you should firstly login. Because there's not much of work, let's make a login page. Not to reinvent the wheel and write it form the beginning we will use <a href="https://github.com/ZF-Commons/ZfcUser">ZfcUser</a>. To do it, just put inside <code>composer.json</code> requirement for <strong>ZfcUser</strong> module</p>
<div class="highlight"><pre><span class="p">{</span>
<span class="nt">"some additional"</span><span class="p">:</span> <span class="s2">"keys"</span><span class="p">,</span>
<span class="nt">"require"</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">"php"</span><span class="p">:</span> <span class="s2">">=5.3.3"</span><span class="p">,</span>
<span class="nt">"zendframework/zsendframework"</span><span class="p">:</span> <span class="s2">"2.*"</span><span class="p">,</span>
<span class="nt">"zf-commons/zfc-user"</span><span class="p">:</span> <span class="s2">"dev-master"</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>and run php composer.phar install. It should install you ZfcUser module.
Now let's enable this module. To do it, edit main configuration file for <strong>ZF2</strong> and make the <strong>modules</strong> array look like</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">///config/application.config.php</span>
<span class="k">return</span> <span class="k">array</span><span class="p">(</span>
<span class="c1">// This should be an array of module namespaces used in the application.</span>
<span class="s1">'modules'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'Application'</span><span class="p">,</span>
<span class="s1">'ZfcBase'</span><span class="p">,</span>
<span class="s1">'ZfcUser'</span><span class="p">,</span>
<span class="p">),</span>
<span class="c1">//other zf2 app settings</span>
<span class="p">);</span>
</pre></div>
<p><strong>ZfcUser</strong> also requires module configuration. You should copy base configuration file <code>/vendor/zf-commons/zfc-user/config/zfc-user.global.php.dist</code>, put it inside <code>/config/autoload/</code> and rename it to <code>zfcuser.global.php</code>. For the sake of this project we will enable also registration, because I would like to register myself nicely instead of writing into database my data. For that, just uncomment line inside <code>zfcuser.global.php</code></p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//...</span>
<span class="s1">'enable_registration'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
<span class="c1">//...</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Last step will be adding user schema into database to store somewhere bunch of users which will register. Create a database called <code>social</code> and run queries on it from a <code>schema.mysql.sql</code> file inside <code>vendor/zf-commons/zfc-user/data/</code> folder.</p>
<p>Don't forget also to setup <strong>ZF2</strong> to use correct database. It'll be done inside <code>/config/autoload/local.php</code>. This file should not get inside the repository, not to store the credentials to database. Update this file to look similar to:</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">return</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'db'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'<username>'</span><span class="p">,</span>
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">'<password>'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">);</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>You should have now working login page. Try to navigate into <code>http://<server_name>/user/login</code>. You should see login form now. Wasn't that hard to make the login/register functionality.</p>
<p>But wait, it's not working yet as it should. It's fine with the login/registration but still I'm able to navigate to any page without a login in first. We will implement the functionality to disallow navigation through all application without firstly logging in. Inside the <code>/modules/Application/Module.php</code> (for simplicity I will use this module which came from Skeleton App) in bootstrap function update the code so it will look as follows</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//...</span>
<span class="k">class</span> <span class="nc">Module</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">onBootstrap</span><span class="p">(</span><span class="nx">MvcEvent</span> <span class="nv">$e</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$e</span><span class="o">-></span><span class="na">getApplication</span><span class="p">()</span><span class="o">-></span><span class="na">getServiceManager</span><span class="p">()</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'translator'</span><span class="p">);</span>
<span class="nv">$eventManager</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getApplication</span><span class="p">()</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
<span class="nv">$moduleRouteListener</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ModuleRouteListener</span><span class="p">();</span>
<span class="nv">$moduleRouteListener</span><span class="o">-></span><span class="na">attach</span><span class="p">(</span><span class="nv">$eventManager</span><span class="p">);</span>
<span class="nv">$eventManager</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getApplication</span><span class="p">()</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
<span class="c1">//nothing's available for non logged user, so redirect him to login page</span>
<span class="nv">$eventManager</span><span class="o">-></span><span class="na">attach</span><span class="p">(</span><span class="s2">"dispatch"</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$sm</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getApplication</span><span class="p">()</span><span class="o">-></span><span class="na">getServiceManager</span><span class="p">();</span>
<span class="nv">$controller</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getTarget</span><span class="p">();</span>
<span class="nv">$auth</span> <span class="o">=</span> <span class="nv">$sm</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'zfcuser_auth_service'</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$auth</span><span class="o">-></span><span class="na">hasIdentity</span><span class="p">()</span> <span class="o">&&</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getRouteMatch</span><span class="p">()</span><span class="o">-></span><span class="na">getMatchedRouteName</span><span class="p">()</span> <span class="o">!==</span> <span class="s1">'zfcuser/login'</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$application</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getTarget</span><span class="p">();</span>
<span class="nv">$e</span><span class="o">-></span><span class="na">stopPropagation</span><span class="p">();</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getResponse</span><span class="p">();</span>
<span class="nv">$response</span><span class="o">-></span><span class="na">setStatusCode</span><span class="p">(</span><span class="mi">302</span><span class="p">);</span>
<span class="nv">$response</span><span class="o">-></span><span class="na">getHeaders</span><span class="p">()</span><span class="o">-></span><span class="na">addHeaderLine</span><span class="p">(</span><span class="s1">'Location'</span><span class="p">,</span> <span class="nv">$e</span><span class="o">-></span><span class="na">getRouter</span><span class="p">()</span><span class="o">-></span><span class="na">assemble</span><span class="p">(</span><span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span><span class="s1">'name'</span> <span class="o">=></span> <span class="s1">'zfcuser/login'</span><span class="p">)));</span>
<span class="c1">//returning response will cause zf2 to stop further dispatch loop</span>
<span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">},</span> <span class="mi">100</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">//...</span>
<span class="p">}</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Now every time you will try to go to any page other than login you will be immediately redirected to login page. And we have already a simple login implemented! Now try to login and enter the site.</p>
<h2>I see a wall?!</h2>
<p>Yes, but it's... empty (or rather you're seeing a ZF2 logo). Let's cleanup it a little. ZF2 already comes with <a href="http://twitter.github.io/bootstrap/">Twitter Bootstrap</a>, so let's us it. We will only alter layout a little. Please open <code>module/Application/view/layout.phtml</code> file (yeah we'll be altering already existing Skeleton App).</p>
<div class="highlight"><pre><span class="cp"><?php echo $this->doctype(); ?></span>
<span class="nt"><html</span> <span class="na">lang=</span><span class="s">"en"</span><span class="nt">></span>
<span class="nt"><head></span>
<span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"utf-8"</span><span class="nt">></span>
<span class="cp"><?php echo $this->headTitle('MY Social Network '. $this->translate('Skeleton Application'))->setSeparator(' - ')->setAutoEscape(false) ?></span>
<span class="cp"><?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0') ?></span>
<span class="c"><!-- Le styles --></span>
<span class="cp"><?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))</span>
<span class="cp"> ->prependStylesheet($this->basePath() . '/css/bootstrap-responsive.min.css')</span>
<span class="cp"> ->prependStylesheet($this->basePath() . '/css/style.css')</span>
<span class="cp"> ->prependStylesheet($this->basePath() . '/css/bootstrap.css') ?></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"navbar navbar-inverse navbar-fixed-top"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"navbar-inner"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"container"</span><span class="nt">></span>
<span class="nt"><a</span> <span class="na">class=</span><span class="s">"btn btn-navbar"</span> <span class="na">data-toggle=</span><span class="s">"collapse"</span> <span class="na">data-target=</span><span class="s">".nav-collapse"</span><span class="nt">></span>
<span class="nt"><span</span> <span class="na">class=</span><span class="s">"icon-bar"</span><span class="nt">></span></span>
<span class="nt"><span</span> <span class="na">class=</span><span class="s">"icon-bar"</span><span class="nt">></span></span>
<span class="nt"><span</span> <span class="na">class=</span><span class="s">"icon-bar"</span><span class="nt">></span></span>
<span class="nt"></a></span>
<span class="nt"><a</span> <span class="na">class=</span><span class="s">"brand"</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('home') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('MY Social Network') ?></span><span class="nt"></a></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"nav-collapse collapse"</span><span class="nt">></span>
<span class="nt"><ul</span> <span class="na">class=</span><span class="s">"nav"</span><span class="nt">></span>
<span class="cp"><?php if ($this->zfcUserIdentity()): ?></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('home') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Home') ?></span><span class="nt"></a></li></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Me') ?></span><span class="nt"></a></li></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser/logout') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Logout') ?></span><span class="nt"></a></li></span>
<span class="cp"><?php else: ?></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser/login') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Login') ?></span><span class="nt"></a></li></span>
<span class="cp"><?php endif; ?></span>
<span class="nt"></ul></span>
<span class="nt"></div></span><span class="c"><!--/.nav-collapse --></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"container"</span> <span class="na">id=</span><span class="s">"container"</span><span class="nt">></span>
<span class="cp"><?php echo $this->content; ?></span>
<span class="nt"><hr></span>
<span class="nt"><footer></span>
<span class="nt"><p></span><span class="ni">&copy;</span> 2013 - <span class="cp"><?php echo date('Y') ?></span> My super company <span class="cp"><?php echo $this->translate('All rights reserved.') ?></span><span class="nt"></p></span>
<span class="nt"></footer></span>
<span class="nt"></div></span> <span class="c"><!-- /container --></span>
<span class="c"><!-- Scripts --></span>
<span class="cp"><?php echo $this->headScript()</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/backbone.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/underscore.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/bootstrap.min.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/jquery.min.js'); ?></span>
<span class="cp"><?php echo $this->inlineScript() ?></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</pre></div>
<p>Here are some important notes. You saw where the scripts went? Almost at the end of body.</p>
<div class="highlight"><pre> <span class="c"><!-- Scripts --></span>
<span class="cp"><?php echo $this->headScript()</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/backbone.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/underscore.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/bootstrap.min.js')</span>
<span class="cp"> ->prependFile($this->basePath() . '/js/jquery.min.js'); ?></span>
<span class="cp"><?php echo $this->inlineScript() ?></span>
</pre></div>
<p>Why you ask? When your social network will be growing you will start adding more and more JavaScript. There are many methods of optimization, but one of the simplest one will be moving your scripts at the end of HTML. This will make browser parse the scripts after showing the rest of the page to an user. There's one minus though - until scripts are loaded user cannot interact with your app. This problem can be solved but I won't be doing it here.
It's important to have them added in the same order. First thing that should be loaded is jQuery. This is one of the dependencies required for <strong>Backbone.js</strong>. Other one is <strong>Underscore.js</strong>. This JavaScript has to be also inside the DOM before <strong>Backbone</strong> (we use here <code>prependFile</code> function). Last included JavaScript is <strong>Backbone.js</strong> itself.</p>
<div class="highlight"><pre><span class="nt"><ul</span> <span class="na">class=</span><span class="s">"nav"</span><span class="nt">></span>
<span class="cp"><?php if ($this->zfcUserIdentity()): ?></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('home') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Home') ?></span><span class="nt"></a></li></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Me') ?></span><span class="nt"></a></li></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser/logout') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Logout') ?></span><span class="nt"></a></li></span>
<span class="cp"><?php else: ?></span>
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"active"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"<?php echo $this->url('zfcuser/login') ?>"</span><span class="nt">></span><span class="cp"><?php echo $this->translate('Login') ?></span><span class="nt"></a></li></span>
<span class="cp"><?php endif; ?></span>
<span class="nt"></ul></span>
</pre></div>
<p>ZfcUser gives you out of the box functionality to check whether user is logged or not. You can do it by calling</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserIdentity</span><span class="p">();</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>in a view file. Right now our social network is really simple and allows only to show wall or user info (this is also provided by <strong>ZfcUser</strong>) and eventually log out.</p>
<p>Let's also add a little bit of CSS styles for later usage (they're really simple not to make the first steps too complex, style the application as you would like).</p>
<div class="highlight"><pre><span class="c">/* public/css/style.css */</span>
<span class="nc">.border-bottom</span> <span class="p">{</span>
<span class="k">border-bottom</span><span class="o">:</span><span class="m">1px</span> <span class="k">solid</span> <span class="m">#ccc</span><span class="p">;</span>
<span class="k">margin-bottom</span><span class="o">:</span> <span class="m">5px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.border-left</span> <span class="p">{</span>
<span class="k">border-left</span><span class="o">:</span><span class="m">1px</span> <span class="k">solid</span> <span class="m">#ccc</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.border-right</span> <span class="p">{</span>
<span class="k">border-right</span><span class="o">:</span><span class="m">1px</span> <span class="k">solid</span> <span class="m">#ccc</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">div</span><span class="nc">.share-form</span> <span class="p">{</span>
<span class="k">padding</span><span class="o">:</span><span class="m">5px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">textarea</span><span class="nf">#share-input</span> <span class="p">{</span>
<span class="k">height</span><span class="o">:</span> <span class="m">40px</span><span class="p">;</span>
<span class="k">width</span><span class="o">:</span> <span class="m">100</span><span class="o">%</span><span class="p">;</span>
<span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="k">border</span><span class="o">-</span><span class="n">radius</span><span class="o">:</span> <span class="m">5px</span><span class="p">;</span>
<span class="k">margin</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
<span class="k">border</span><span class="o">-</span><span class="n">radius</span><span class="o">:</span> <span class="m">5px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">div</span><span class="nc">.post</span> <span class="p">{</span>
<span class="k">padding</span><span class="o">:</span> <span class="m">10px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">div</span><span class="nc">.post-footer</span> <span class="p">{</span>
<span class="k">color</span><span class="o">:</span> <span class="m">#3b5998</span><span class="p">;</span>
<span class="k">font-size</span><span class="o">:</span> <span class="m">12px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">p</span><span class="nc">.post-author</span> <span class="p">{</span>
<span class="k">color</span><span class="o">:</span> <span class="m">#3b5998</span><span class="p">;</span>
<span class="p">}</span>
<span class="nc">.btn-share</span> <span class="p">{</span>
<span class="k">float</span><span class="o">:</span><span class="k">right</span><span class="p">;</span>
<span class="k">margin</span><span class="o">:</span> <span class="m">5px</span> <span class="m">0</span> <span class="m">5px</span> <span class="m">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">span</span><span class="nc">.post-like-likes</span> <span class="p">{</span>
<span class="k">position</span><span class="o">:</span> <span class="k">absolute</span><span class="p">;</span>
<span class="k">padding</span><span class="o">:</span> <span class="m">4px</span><span class="p">;</span>
<span class="k">background-color</span><span class="o">:</span> <span class="m">#000</span><span class="p">;</span>
<span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span>
<span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">5px</span> <span class="m">5px</span> <span class="m">2px</span> <span class="m">#aaa</span><span class="p">;</span>
<span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">5px</span> <span class="m">5px</span> <span class="m">2px</span> <span class="m">#aaa</span><span class="p">;</span>
<span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">5px</span> <span class="m">5px</span> <span class="m">2px</span> <span class="m">#aaa</span><span class="p">;</span>
<span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="k">border</span><span class="o">-</span><span class="n">radius</span><span class="o">:</span> <span class="m">5px</span><span class="p">;</span>
<span class="k">border</span><span class="o">-</span><span class="n">radius</span><span class="o">:</span> <span class="m">2px</span><span class="p">;</span>
<span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>Ok, so we can assume that layout is done. Now you should see simple menu with 3 tabs on the top, but still no posts.</p>
<h3>My share form</h3>
<p>What kind of social network would be without a share form? We need one. Let's do it then.</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//module/Application/src/Application/Form/ShareForm.php</span>
<span class="o"><?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Application\Form</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Form\Form</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">ShareForm</span> <span class="k">extends</span> <span class="nx">Form</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">parent</span><span class="o">::</span><span class="na">__construct</span><span class="p">(</span><span class="s1">'share'</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">add</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span>
<span class="s1">'name'</span> <span class="o">=></span> <span class="s1">'share'</span><span class="p">,</span>
<span class="s1">'options'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'label'</span> <span class="o">=></span> <span class="s2">"Status"</span><span class="p">,</span>
<span class="p">),</span>
<span class="s1">'attributes'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'type'</span> <span class="o">=></span> <span class="s1">'textarea'</span><span class="p">,</span>
<span class="s1">'id'</span> <span class="o">=></span> <span class="s1">'share-input'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">)</span>
<span class="p">);</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">add</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span>
<span class="s1">'name'</span> <span class="o">=></span> <span class="s1">'submit'</span><span class="p">,</span>
<span class="s1">'attributes'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'type'</span> <span class="o">=></span> <span class="s1">'submit'</span><span class="p">,</span>
<span class="s1">'value'</span> <span class="o">=></span> <span class="s1">'Share'</span><span class="p">,</span>
<span class="s1">'class'</span> <span class="o">=></span> <span class="s1">'btn btn-primary btn-share'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">)</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>This will create a simple form with a textarea and a submit button. We will use later <strong>id</strong> attribute to attach events to this form. Also not that the form will be without any <strong>Filter</strong> nor validation. In a production application validation should be added (ZF2 Forms support it really nicely).</p>
<p>Next step will be putting this form inside view for our wall (till now only layout exists). Create a view inside of <code>module/Application/src/view/application/index</code> and name it <code>index.phtml</code>.</p>
<div class="highlight"><pre><span class="x"><div class="container"></span>
<span class="x"> <div class="row"></span>
<span class="x"> <div class="span1"></span>
<span class="x"> <p>sidebar left</p></span>
<span class="x"> </div></span>
<span class="x"> <div class="content span6 border-left"></span>
<span class="x"> <div class="border-bottom share-form"></span>
<span class="x"> </span><span class="cp"><?php</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">prepare</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'action'</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-></span><span class="na">url</span><span class="p">(</span><span class="s1">'application/default'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'controller'</span> <span class="o">=></span> <span class="s1">'share'</span><span class="p">,</span> <span class="s1">'action'</span> <span class="o">=></span> <span class="s1">'add'</span><span class="p">)));</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'id'</span><span class="p">,</span> <span class="s1">'share-form'</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'method'</span><span class="p">,</span> <span class="s1">'post'</span><span class="p">);</span>
<span class="c1">//open form</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="p">()</span><span class="o">-></span><span class="na">openTag</span><span class="p">(</span><span class="nv">$form</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">formRow</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'share'</span><span class="p">));</span>
<span class="k">echo</span> <span class="s1">'<br />'</span><span class="p">;</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">formRow</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'submit'</span><span class="p">));</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="p">()</span><span class="o">-></span><span class="na">closeTag</span><span class="p">();</span>
<span class="cp">?></span><span class="x"></span>
<span class="x"> <br clear:both; /></span>
<span class="x"> </div></span>
<span class="x"> </div></span>
<span class="x"> <div class="span2"></span>
<span class="x"> <p>sidebar right</p></span>
<span class="x"> </div></span>
<span class="x"> </div></span>
<span class="x"></div></span>
</pre></div>
<p>In the code above you will see a 3 column layout for the wall page (later you will want probably to put there some additional navbar). For now left and right sidebars will be empty.</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'action'</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-></span><span class="na">url</span><span class="p">(</span><span class="s1">'application/default'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'controller'</span> <span class="o">=></span> <span class="s1">'share'</span><span class="p">,</span> <span class="s1">'action'</span> <span class="o">=></span> <span class="s1">'add'</span><span class="p">)));</span>
<span class="cp">?></span><span class="x"></span>
</pre></div>
<p>Also already an url for a form was added (for now not working) where your new shares will be put. This will generate a form with textarea and a button under it, but the form is still not inside the view. Later we will pass it to the view from controller with all shares. If you enter the index page there will be and error so wait some more time.</p>
<h3>Models to get'em all</h3>
<p>To obtain shares of users model will be necessary. For simplicity there will be only one table, and you will be able to see for now (sadly) only your shares. Create schema inside your <strong>social</strong> database.</p>
<div class="highlight"><pre><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="o">`</span><span class="k">share</span><span class="o">`</span> <span class="p">(</span>
<span class="o">`</span><span class="n">share_id</span><span class="o">`</span> <span class="nb">int</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span> <span class="n">unsigned</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="n">AUTO_INCREMENT</span><span class="p">,</span>
<span class="o">`</span><span class="n">user_id</span><span class="o">`</span> <span class="nb">int</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span> <span class="n">unsigned</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">COMMENT</span> <span class="s1">'User id of an the posts should be visible'</span><span class="p">,</span>
<span class="o">`</span><span class="n">author_id</span><span class="o">`</span> <span class="nb">int</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span> <span class="n">unsigned</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="n">author</span><span class="o">`</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">COMMENT</span> <span class="s1">'Name of author of post'</span><span class="p">,</span>
<span class="o">`</span><span class="nb">text</span><span class="o">`</span> <span class="nb">text</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">COMMENT</span> <span class="s1">'Share text'</span><span class="p">,</span>
<span class="o">`</span><span class="k">type</span><span class="o">`</span> <span class="n">tinyint</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">DEFAULT</span> <span class="s1">'0'</span> <span class="k">COMMENT</span> <span class="s1">'Has link'</span><span class="p">,</span>
<span class="o">`</span><span class="n">thumbnail</span><span class="o">`</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span> <span class="k">DEFAULT</span> <span class="k">NULL</span> <span class="k">COMMENT</span> <span class="s1">'Optional thumbnail'</span><span class="p">,</span>
<span class="o">`</span><span class="n">added</span><span class="o">`</span> <span class="n">datetime</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="n">likes</span><span class="o">`</span> <span class="nb">int</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="n">unsigned</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">DEFAULT</span> <span class="s1">'0'</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">share_id</span><span class="o">`</span><span class="p">),</span>
<span class="k">KEY</span> <span class="o">`</span><span class="n">user_idx</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">user_id</span><span class="o">`</span><span class="p">),</span>
<span class="k">KEY</span> <span class="o">`</span><span class="n">author_idx</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">author_id</span><span class="o">`</span><span class="p">),</span>
<span class="k">KEY</span> <span class="o">`</span><span class="n">user_id_added</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">user_id</span><span class="o">`</span><span class="p">,</span><span class="o">`</span><span class="n">added</span><span class="o">`</span><span class="p">),</span>
<span class="k">CONSTRAINT</span> <span class="o">`</span><span class="n">author</span><span class="o">`</span> <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">author_id</span><span class="o">`</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="o">`</span><span class="n">users</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">user_id</span><span class="o">`</span><span class="p">)</span> <span class="k">ON</span> <span class="k">DELETE</span> <span class="k">CASCADE</span> <span class="k">ON</span> <span class="k">UPDATE</span> <span class="k">NO</span> <span class="n">ACTION</span><span class="p">,</span>
<span class="k">CONSTRAINT</span> <span class="o">`</span><span class="k">user</span><span class="o">`</span> <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">user_id</span><span class="o">`</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="o">`</span><span class="n">users</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">user_id</span><span class="o">`</span><span class="p">)</span> <span class="k">ON</span> <span class="k">DELETE</span> <span class="k">CASCADE</span> <span class="k">ON</span> <span class="k">UPDATE</span> <span class="k">NO</span> <span class="n">ACTION</span>
<span class="p">)</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">InnoDB</span> <span class="n">AUTO_INCREMENT</span><span class="o">=</span><span class="mi">1</span> <span class="k">DEFAULT</span> <span class="n">CHARSET</span><span class="o">=</span><span class="n">utf8</span>
</pre></div>
<p>With each share there will be stored likes counter. It is necessary to already show how many likes one share will have without using counts for each post. Now let's create a <strong>ShareTable</strong> which will be a <strong>TableGateway</strong></p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Application\Model</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Db\TableGateway\TableGateway</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Db\Sql</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">ShareTable</span> <span class="k">extends</span> <span class="nx">TableGateway</span>
<span class="p">{</span>
<span class="k">protected</span> <span class="nv">$tableGateway</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nx">TableGateway</span> <span class="nv">$tableGateway</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">tableGateway</span> <span class="o">=</span> <span class="nv">$tableGateway</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getByUserId</span><span class="p">(</span><span class="nv">$userId</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">tableGateway</span><span class="o">-></span><span class="na">select</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="nx">Sql\Select</span> <span class="nv">$select</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$userId</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$select</span>
<span class="o">-></span><span class="na">order</span><span class="p">(</span><span class="s1">'added DESC'</span><span class="p">)</span>
<span class="o">-></span><span class="na">limit</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="o">-></span><span class="na">where</span>
<span class="o">-></span><span class="na">equalTo</span><span class="p">(</span><span class="s1">'user_id'</span><span class="p">,</span> <span class="nv">$userId</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">insert</span><span class="p">(</span><span class="nv">$data</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">tableGateway</span><span class="o">-></span><span class="na">insert</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'user_id'</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">[</span><span class="s1">'user_id'</span><span class="p">],</span>
<span class="s1">'author_id'</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">[</span><span class="s1">'author_id'</span><span class="p">],</span>
<span class="s1">'author'</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">[</span><span class="s1">'author'</span><span class="p">],</span>
<span class="s1">'text'</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">[</span><span class="s1">'text'</span><span class="p">],</span>
<span class="s1">'type'</span> <span class="o">=></span> <span class="nv">$data</span><span class="p">[</span><span class="s1">'type'</span><span class="p">],</span>
<span class="s1">'added'</span> <span class="o">=></span> <span class="k">new</span> <span class="nx">Sql\Expression</span><span class="p">(</span><span class="s1">'NOW()'</span><span class="p">),</span>
<span class="p">));</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">tableGateway</span><span class="o">-></span><span class="na">lastInsertValue</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">updateLikes</span><span class="p">(</span><span class="nv">$shareId</span><span class="p">,</span> <span class="nv">$amount</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">tableGateway</span><span class="o">-></span><span class="na">update</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'likes'</span> <span class="o">=></span> <span class="k">new</span> <span class="nx">\Zend\Db\Sql\Expression</span><span class="p">(</span><span class="s1">'likes + '</span> <span class="o">.</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span><span class="nv">$amount</span><span class="p">)</span>
<span class="p">),</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'share_id'</span> <span class="o">=></span> <span class="nv">$shareId</span><span class="p">)</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Hope that the code is self explanatory this time.</p>
<p>Because the same model can be used later in different places inside our module, the best way will be registering it as a factory. This will allow you to use the same instance of ShareTable across the system. Let's register it as a factory, cause it's dependent on TableGateway, which is dependent on <code>Zend\Db\Adapter\Adapter</code>. Open <code>module/Application/Module.php</code> and make <code>getServiceConfig()</code> function to look as follows</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//module/Application/Module.php</span>
<span class="k">namespace</span> <span class="nx">Application</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Mvc\ModuleRouteListener</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Mvc\MvcEvent</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Db\TableGateway\TableGateway</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\Model</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\View</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Module</span>
<span class="p">{</span>
<span class="c1">//...</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getServiceConfig</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'factories'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'Application\Model\ShareTable'</span> <span class="o">=></span> <span class="k">function</span> <span class="p">(</span><span class="nv">$sm</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$tableGateway</span> <span class="o">=</span> <span class="nv">$sm</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'ShareTableGateway'</span><span class="p">);</span>
<span class="nv">$table</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Model\ShareTable</span><span class="p">(</span><span class="nv">$tableGateway</span><span class="p">);</span>
<span class="k">return</span> <span class="nv">$table</span><span class="p">;</span>
<span class="p">},</span>
<span class="s1">'ShareTableGateway'</span> <span class="o">=></span> <span class="k">function</span> <span class="p">(</span><span class="nv">$sm</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$dbAdapter</span> <span class="o">=</span> <span class="nv">$sm</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'Zend\Db\Adapter\Adapter'</span><span class="p">);</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">TableGateway</span><span class="p">(</span><span class="s1">'share'</span><span class="p">,</span> <span class="nv">$dbAdapter</span><span class="p">);</span>
<span class="p">},</span>
<span class="p">)</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>So we have now 2 services <code>Application\Model\ShareTable</code> and <code>ShareTableGateway</code>. Also a nice idea would be registering prototype row for <code>ShareTableGateway</code> but we'll omit that for now.</p>
<h3>Controller for gluing all the pieces together</h3>
<p>Now it's time for our controller to glue everything and start viewing some shares (if there are any).</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//module/Application/src/Application/Controller/IndexController.php</span>
<span class="k">namespace</span> <span class="nx">Application\Controller</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\Form</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\Model</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">IndexController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
<span class="k">protected</span> <span class="nv">$shareTable</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$form</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Form\ShareForm</span><span class="p">();</span>
<span class="nv">$shareTable</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getShareTable</span><span class="p">();</span>
<span class="nv">$userId</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserAuthentication</span><span class="p">()</span><span class="o">-></span><span class="na">getIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getId</span><span class="p">();</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">ViewModel</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span>
<span class="s1">'form'</span> <span class="o">=></span> <span class="nv">$form</span><span class="p">,</span>
<span class="s1">'posts'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">getShareTable</span><span class="p">()</span><span class="o">-></span><span class="na">getByUserId</span><span class="p">(</span><span class="nv">$userId</span><span class="p">),</span>
<span class="p">)</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getShareTable</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$sm</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getServiceLocator</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span> <span class="o">=</span> <span class="nv">$sm</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'Application\Model\ShareTable'</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Function <code>indexAction</code> will render our wall with all shares that are specified for currently logged user. Note that here we use <strong>Inversion of Control</strong> for our share table.</p>
<p>The last step will be altering our view by adding some code responsible for viewing each share. Modify the <code>module/Application/view/index/index.phtml</code> file so it will now look like</p>
<div class="highlight"><pre><span class="x"><div class="container"></span>
<span class="x"> <div class="row"></span>
<span class="x"> <div class="span1"></span>
<span class="x"> <p>sidebar left</p></span>
<span class="x"> </div></span>
<span class="x"> <div class="content span6 border-left"></span>
<span class="x"> <div class="border-bottom share-form"></span>
<span class="x"> </span><span class="cp"><?php</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">prepare</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'action'</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-></span><span class="na">url</span><span class="p">(</span><span class="s1">'application/default'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'controller'</span> <span class="o">=></span> <span class="s1">'share'</span><span class="p">,</span> <span class="s1">'action'</span> <span class="o">=></span> <span class="s1">'add'</span><span class="p">)));</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'id'</span><span class="p">,</span> <span class="s1">'share-form'</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">setAttribute</span><span class="p">(</span><span class="s1">'method'</span><span class="p">,</span> <span class="s1">'post'</span><span class="p">);</span>
<span class="c1">//open form</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="p">()</span><span class="o">-></span><span class="na">openTag</span><span class="p">(</span><span class="nv">$form</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">formRow</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'share'</span><span class="p">));</span>
<span class="k">echo</span> <span class="s1">'<br />'</span><span class="p">;</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">formRow</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'submit'</span><span class="p">));</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">form</span><span class="p">()</span><span class="o">-></span><span class="na">closeTag</span><span class="p">();</span>
<span class="cp">?></span><span class="x"></span>
<span class="x"> <br clear:both; /></span>
<span class="x"> </div></span>
<span class="x"> <div id="post-list"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">posts</span> <span class="k">as</span> <span class="nv">$post</span><span class="p">)</span><span class="o">:</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">partial</span><span class="p">(</span><span class="s1">'index/partials/post.phtml'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'post'</span> <span class="o">=></span> <span class="nv">$post</span><span class="p">));</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">endforeach</span><span class="p">;</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </div></span>
<span class="x"> </div></span>
<span class="x"> <div class="span2"></span>
<span class="x"> <p>sidebar right</p></span>
<span class="x"> </div></span>
<span class="x"> </div></span>
<span class="x"></div></span>
</pre></div>
<p>Each post will be rendered by partial. This is because the view will be cleaner and easier to read this way. Additionally it's because of <strong>DRY</strong>. We will reuse this partial later for JavaScript. This partial looks like</p>
<div class="highlight"><pre><span class="x"><div class="post" id="post_</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'share_id'</span><span class="p">]</span><span class="cp">?></span><span class="x">" ></span>
<span class="x"> <div class="border-bottom"></span>
<span class="x"> <div></span>
<span class="x"> <p></span>
<span class="x"> <span class="gravatar"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'gravatar'</span><span class="p">]))</span><span class="o">:</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">gravatar</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'author'</span><span class="p">],</span> <span class="k">array</span><span class="p">(</span><span class="s1">'img_size'</span> <span class="o">=></span> <span class="mi">40</span><span class="p">));</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">else</span><span class="o">:</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'gravatar'</span><span class="p">];</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">endif</span><span class="p">;</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span></span>
<span class="x"> <span class="post-author"></span>
<span class="x"> </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'author'</span><span class="p">];</span> <span class="cp">?></span><span class="x"></span>
<span class="x"> </span></span>
<span class="x"> </p></span>
<span class="x"> </div></span>
<span class="x"> <p class="post-text"></span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'text'</span><span class="p">];</span> <span class="cp">?></span><span class="x"></p></span>
<span class="x"> <div class="post-footer"></span>
<span class="x"> <span class="post-like"></span>
<span class="x"> <a class="post-like-button">Like!</a></span>
<span class="x"> <span class="post-like-likes"></span></span>
<span class="x"> <span class="post-likes-count"></span><span class="cp"><?php</span> <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'likes'</span><span class="p">])</span><span class="o">:</span> <span class="cp">?></span><span class="x">(</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'likes'</span><span class="p">];</span> <span class="cp">?></span><span class="x">)</span><span class="cp"><?php</span> <span class="k">endif</span><span class="p">;</span> <span class="cp">?></span><span class="x"></span></span>
<span class="x"> </span></span>
<span class="x"> &middot; </span>
<span class="x"> <span class="post-date"></span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">post</span><span class="p">[</span><span class="s1">'added'</span><span class="p">];</span> <span class="cp">?></span><span class="x"></span></span>
<span class="x"> </div></span>
<span class="x"> </div></span>
<span class="x"></div></span>
</pre></div>
<p>As you can see we will be showing a gravatar, author and share text</p>
<p>If you will add one record to shares table you should already see a new share. But still we cannot create new shares from our Social Network.</p>
<h3>It's time for some JavaScript action</h3>
<p>Now let's make some action with backbone. Let's create new file <code>share.js</code> and put it inside <code>public/javascript</code> folder. Let's also add this file to included JavaScript list. It can be done by placing on the beginning of <code>module/Application/view/index/index.phtml</code> a line</p>
<div class="highlight"><pre><span class="cp"><?php</span> <span class="nv">$this</span><span class="o">-></span><span class="na">headScript</span><span class="p">()</span><span class="o">-></span><span class="na">appendFile</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">basePath</span><span class="p">()</span> <span class="o">.</span> <span class="s1">'js/share.js'</span><span class="p">);</span> <span class="cp">?></span><span class="x"></span>
</pre></div>
<p>This will do what we need (again for the simplicity there will be only one file, normally you would divide this app into more files and during the deployment merge them correctly or use something like Require.js). Also be sure to use this time (in opposition to layout) <code>appendFile</code> function to be sure that this script will be loaded <strong>after</strong> Backbone.js.</p>
<p>Let's start editing <code>share.js</code> file and put some JS into an action. Firstly let's create new module and main application object with namespaces for it's parts.</p>
<div class="highlight"><pre><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">share</span><span class="p">,</span> <span class="nx">global</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">$</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">){</span>
<span class="s1">'use strict'</span><span class="p">;</span>
<span class="c1">//application - manages input and send messages to collection</span>
<span class="kd">var</span> <span class="nx">AppView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#container'</span><span class="p">)</span>
<span class="p">});</span>
<span class="nx">AppView</span><span class="p">.</span><span class="nx">models</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">AppView</span><span class="p">.</span><span class="nx">collections</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">AppView</span><span class="p">.</span><span class="nx">views</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">$</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">new</span> <span class="nx">AppView</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">})({},</span> <span class="k">this</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">$</span><span class="p">,</span> <span class="nx">user</span><span class="p">);</span>
</pre></div>
<p>Our main application object is an Backbone view! This way we'll be able to 'glue' to already existing DOM and add some events for already existing elements. To make it glued to the already existing DOM Backbone requires to put <code>el: domElement</code> inside the view (it's also possible to specify this element while constructing objects).</p>
<p>Additionally there were created 3 namespaces to organize our code a little (models, collections, views).</p>
<p>When you use Backbone you divide your code between <strong>Views, Models and Collections</strong> Model's responsible for logic and storing data. Collection is just a container of many models. View at the end is responsible for rendering HTML.</p>
<p>Firstly let's create a model for a single share entry.</p>
<div class="highlight"><pre><span class="nx">AppView</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">ShareModel</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="p">});</span>
</pre></div>
<p>And a view</p>
<div class="highlight"><pre><span class="nx">AppView</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">ShareView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="nx">AppView</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">ShareModel</span><span class="p">,</span>
<span class="nx">tagName</span><span class="o">:</span> <span class="s1">'div'</span><span class="p">,</span>
<span class="nx">template</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s1">'#post-template'</span><span class="p">).</span><span class="nx">html</span><span class="p">()),</span>
<span class="nx">className</span><span class="o">:</span> <span class="s1">'post'</span><span class="p">,</span>
<span class="nx">attributes</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span>
<span class="nx">id</span><span class="o">:</span> <span class="s1">'post_'</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'share_id'</span><span class="p">),</span>
<span class="p">};</span>
<span class="p">},</span>
<span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">listenTo</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span> <span class="s1">'change'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">listenTo</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span> <span class="s1">'destroy'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">remove</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span>
<span class="p">},</span>
<span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">attrs</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">attributes</span><span class="p">);</span>
<span class="c1">//print readable date</span>
<span class="nx">attrs</span><span class="p">.</span><span class="nx">added</span> <span class="o">=</span> <span class="nx">attrs</span><span class="p">.</span><span class="nx">added</span><span class="p">.</span><span class="nx">toLocaleString</span><span class="p">();</span>
<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span>
<span class="nx">$</span><span class="p">.</span><span class="nx">parseHTML</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">attrs</span><span class="p">))[</span><span class="mi">1</span><span class="p">]</span>
<span class="p">.</span><span class="nx">innerHTML</span>
<span class="p">);</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">hideLikes</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">likesList</span><span class="p">.</span><span class="nx">remove</span><span class="p">();</span>
<span class="p">},</span>
<span class="nx">remove</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">remove</span><span class="p">();</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div>
<p>There's a bunch of code, so let's stop a little.</p>
<p>It's really common that each view in Backbone has it's own model. By putting inside of view definition</p>
<div class="highlight"><pre><span class="c1">//...</span>
<span class="nx">model</span><span class="o">:</span> <span class="nx">AppView</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">ShareModel</span>
</pre></div>
<p>we define that the default model for such a view is <code>AppView.models.ShareModel</code></p>
<p>Next thing is <code>tagName</code>, this just tells backbone to use <code>div</code> as a main tag for element (each view has it's own 'container'). Let's skip few lines and firstly look into class constructor. In Backbone constructor is an <code>initialize</code> method.</p>
<div class="highlight"><pre><span class="c1">//...</span>
<span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">listenTo</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span> <span class="s1">'change'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">listenTo</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span> <span class="s1">'destroy'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">remove</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span>
<span class="p">},</span>
</pre></div>
<p>This will make us life easier. Each time there's any change inside of the model, the view will be informed. So if for e.g. Edit functionality will be implemented and model will be altered, then the view will be automatically updated.
Right now also in case when model is destroyed, view with a share will be removed from DOM. Because of the <code>this.render()</code> function code each time new share view will be created immediately it will go into the DOM.</p>
<p>Now let's analyze render and template function. Backbone.js uses by default underscore templating mechanism. You can create templates in at least two ways</p>
<ul>
<li>Create template server side. This template will go inside the HTML page and will be obtained from JavaScript.</li>
<li>Create template inside your JS file with a view.</li>
</ul>
<p>Which method to use, differs from situation. For me important is <strong>DRY</strong>. The worst thing is making repetitions of templates and creating two, one for server side rendering, second for dynamically created content. This way you will end altering twice your templates in case of any change. So here I've decided to reuse my partial for each share.</p>
<p>Somewhere at the end of <code>module/Application/view/index/index.phtml</code> put this code</p>
<div class="highlight"><pre><span class="x"><script type="text/template" id="post-template"></span>
<span class="cp"><?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-></span><span class="na">partial</span><span class="p">(</span><span class="s1">'index/partials/post.phtml'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'post'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">templateVars</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'share_id'</span><span class="p">,</span>
<span class="s1">'author'</span><span class="p">,</span>
<span class="s1">'text'</span><span class="p">,</span>
<span class="s1">'added'</span><span class="p">,</span>
<span class="s1">'likes'</span><span class="p">,</span>
<span class="p">))</span> <span class="o">+</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'gravatar'</span> <span class="o">=></span> <span class="s1">'<img src="<%- gravatar %>" />'</span><span class="p">,</span>
<span class="p">)</span>
<span class="p">)</span>
<span class="p">);</span> <span class="cp">?></span><span class="x"></span>
<span class="x"></script></span>
<span class="x"><script type="text/javascript"></span>
<span class="x">var user = {</span>
<span class="x"> email: </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nb">json_encode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getEmail</span><span class="p">());</span> <span class="cp">?></span><span class="x">,</span>
<span class="x"> id: </span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nb">json_encode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getId</span><span class="p">());</span> <span class="cp">?></span><span class="x"></span>
<span class="x">}</span>
<span class="x"></script></span>
</pre></div>
<p>This way a template for Backbone will be created and it fully reuses previously defined template for a single share! Also here, we've passed some info for JavaScript about the currently logged user.</p>
<p>If you look closely you will see <code>$this->templateVars(..)</code> call. This is a custom helper for helping us with Underscore templates.
Normally underscore requires templates like</p>
<div class="highlight"><pre><span class="o"><%-</span> <span class="nx">variable_name</span> <span class="o">%></span>
</pre></div>
<p>Because our partial waits for model like</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">array</span><span class="p">(</span>
<span class="s1">'text'</span> <span class="o">=></span> <span class="s1">'here goes some text'</span><span class="p">,</span>
<span class="c1">//...</span>
<span class="p">);</span>
</pre></div>
<p>Let's cheat it a little and pass him an array like</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">array</span><span class="p">(</span>
<span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'<%- field %>'</span>
<span class="p">);</span>
</pre></div>
<p>Let's create then <strong>TemplateVars</strong> helper by putting class inside of the <code>module/Application/src/Application/View/Helper/TemplateVars</code></p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//module/Application/src/Application/View/Helper/TemplateVars</span>
<span class="k">namespace</span> <span class="nx">Application\View\Helper</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Http\Request</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Helper\AbstractHelper</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">TemplateVars</span> <span class="k">extends</span> <span class="nx">AbstractHelper</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * Changes variables array into backbone's template vars syntax</span>
<span class="sd"> *</span>
<span class="sd"> * @param array $variables Template variables names</span>
<span class="sd"> *</span>
<span class="sd"> * @return array Array of template vars</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">__invoke</span><span class="p">(</span><span class="k">array</span> <span class="nv">$variables</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$output</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$variables</span> <span class="k">as</span> <span class="nv">$var</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$output</span><span class="p">[</span><span class="nv">$var</span><span class="p">]</span> <span class="o">=</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s1">'<%%- %s %%>'</span><span class="p">,</span> <span class="nv">$var</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nv">$output</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Additionally it's required to register it for view, put this code inside <code>module.config.php</code></p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">//...</span>
<span class="k">return</span> <span class="k">array</span><span class="p">(</span>
<span class="c1">//....</span>
<span class="s1">'view_helpers'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'invokables'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'templateVars'</span> <span class="o">=></span> <span class="s1">'Application\View\Helper\TemplateVars'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">),</span>
<span class="p">);</span>
</pre></div>
<p>Now we can come back to our Backbone view. Remember</p>
<div class="highlight"><pre><span class="nx">template</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s1">'#post-template'</span><span class="p">).</span><span class="nx">html</span><span class="p">())</span>
</pre></div>
<p>This way our view will obtain a template for each share. Having our template it's possible now to render each new share!</p>
<p>Here comes one of the Backbone.js flaws. It requires a lot of boilerplate. Backbone views doesn't know how to render themselves. It's necessary to create a render function. As it was shown before, render function looked like:</p>
<div class="highlight"><pre><span class="nx">render</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">attrs</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">attributes</span><span class="p">);</span>
<span class="c1">//print readable date</span>
<span class="nx">attrs</span><span class="p">.</span><span class="nx">added</span> <span class="o">=</span> <span class="nx">attrs</span><span class="p">.</span><span class="nx">added</span><span class="p">.</span><span class="nx">toLocaleString</span><span class="p">();</span>
<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span>
<span class="nx">$</span><span class="p">.</span><span class="nx">parseHTML</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">template</span><span class="p">(</span><span class="nx">attrs</span><span class="p">))[</span><span class="mi">1</span><span class="p">]</span>
<span class="p">.</span><span class="nx">innerHTML</span>
<span class="p">);</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">},</span>
</pre></div>
<p>Rendering almost always goes this way
* Take the model
* Pass it to the template
* Set is as the main element content (<code>this.$el.html(...)</code>)</p>
<p>Additionally two tricks were done here. One is to change the raw <code>Date</code> into some more readable one. It's done by calling <code>toLocaleString()</code>. Still it would be bad to loose the raw date object because it might be necessary to check later the date of post, that's why it was done not on original model attributes but on it's copy.</p>
<p>Second trick is that we take innerHTML of rendered template. If it wouldn't be done here then our structure would be different than the prerenderred posts. It would look like:</p>
<div class="highlight"><pre><span class="nt"><div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"post"</span> <span class="na">id=</span><span class="s">"post_<some_id>"</span><span class="nt">></span>
//....
<span class="nt"></div></span>
<span class="nt"></div></span>
</pre></div>
<p>Calling innerHTML will take only contents of <code><div class="post"></div></code></p>
<p>To store inside the DOM also share id of newly created share <strong>className</strong> and <strong>attributes</strong> were defined. Attributes are a function because they're dynamically created. In other way you would finish with the same id in every view created now or later (because attribute would be created JavaScript parse time).</p>
<p>In Backbone it's also quite common to define a list of views. This is an additional View object which handles whole list of single views. This kind of 'View Lists' are mostly bound to collections instead of model. In our case we will create <strong>ShareViewList</strong> view which will hold whole list of shares.</p>
<div class="highlight"><pre><span class="c1">//represents view of whole wall</span>
<span class="nx">AppView</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">ShareViewList</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#post-list'</span><span class="p">),</span>
<span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">listenTo</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">,</span> <span class="s1">'add'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">add</span><span class="p">);</span>
<span class="c1">//initialize already existing views in DOM</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">len</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">element</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">models</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="k">new</span> <span class="nx">AppView</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">ShareView</span><span class="p">({</span><span class="nx">model</span><span class="o">:</span> <span class="nx">element</span><span class="p">,</span> <span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#post_'</span> <span class="o">+</span> <span class="nx">element</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'share_id'</span><span class="p">))});</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="nx">add</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AppView</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">ShareView</span><span class="p">({</span><span class="nx">model</span><span class="o">:</span> <span class="nx">data</span><span class="p">,</span> <span class="nx">id</span><span class="o">:</span> <span class="nx">data</span><span class="p">.</span><span class="nx">share_id</span><span class="p">});</span>
<span class="k">this</span><span class="p">.</span><span class="nx">$el</span><span class="p">.</span><span class="nx">prepend</span><span class="p">(</span><span class="nx">view</span><span class="p">.</span><span class="nx">el</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div>
<p>Let's stop on important things here. One thing is that we listen to Collection <strong>add</strong> event here. Every time something is added to a collection it will create new ShareView and this way new HTML will be created.
Second thing is that during creating an instance of this class collection will be searched for already created shares (for loop). Thanks to it, all already created shares will have it's View inside of JS (and all events which ShareView defines attached).</p>
<p>Let's move now to ShareCollection which looks as follows</p>
<div class="highlight"><pre><span class="c1">//represents whole wall of posts</span>
<span class="nx">AppView</span><span class="p">.</span><span class="nx">collections</span><span class="p">.</span><span class="nx">ShareCollection</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="nx">AppView</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">ShareModel</span><span class="p">,</span>
<span class="nx">comparator</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">added</span> <span class="o">></span> <span class="nx">b</span><span class="p">.</span><span class="nx">added</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">list</span> <span class="o">=</span> <span class="p">[],</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="nx">$</span><span class="p">(</span><span class="s1">'#post-list > .post'</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">el</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">$likesEl</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'.post-likes-count'</span><span class="p">);</span>
<span class="nx">list</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="k">new</span> <span class="nx">self</span><span class="p">.</span><span class="nx">model</span><span class="p">({</span>
<span class="nx">share_id</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">'id'</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/post_/</span><span class="p">,</span> <span class="s1">''</span><span class="p">),</span>
<span class="nx">author</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'.post-author'</span><span class="p">).</span><span class="nx">text</span><span class="p">(),</span>
<span class="nx">text</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'.post-text'</span><span class="p">).</span><span class="nx">text</span><span class="p">(),</span>
<span class="nx">added</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'.post-date'</span><span class="p">).</span><span class="nx">text</span><span class="p">()),</span>
<span class="nx">gravatar</span><span class="o">:</span> <span class="nx">get_gravatar</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="nx">el</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'.post-author'</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">trim</span><span class="p">(),</span> <span class="mi">40</span><span class="p">),</span>
<span class="nx">likes</span><span class="o">:</span> <span class="nx">$likesEl</span><span class="p">.</span><span class="nx">text</span><span class="p">()</span> <span class="o">!==</span> <span class="s1">''</span> <span class="o">?</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">$likesEl</span><span class="p">.</span><span class="nx">text</span><span class="p">().</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/[^0-9]+/g</span><span class="p">,</span> <span class="s1">''</span><span class="p">))</span> <span class="o">:</span> <span class="mi">0</span>
<span class="p">}))</span>
<span class="p">});</span>
<span class="k">this</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">list</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div>
<p>This collection when it's created will try to look inside the DOM for already created shares (which are later used inside the ShareViewList to create single views). This way you will have data that is already inside of your HTML (to be honest I think this code should go inside the ShareViewList to encapsulate it completely there, but I didn't have enough time to refactor it).</p>
<p>There's an information for Backbone that this collection will use <code>model: AppView.models.ShareModel</code>. This way you know what kind of model to expect.</p>
<p>I've also used here get_gravatar function (put it at the beginning of our module or in separate file) found <a href="http://www.deluxeblogtips.com/2010/04/get-gravatar-using-only-javascript.html">here</a>.</p>
<h3>Some server side PHP</h3>
<p>Ok, so basically we have some code to create new Views representing each share, but still they do not do anything. Let's create some code responsible for adding new shares.</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Application\Controller</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\Mvc\Controller\AbstractActionController</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Zend\View\Model\ViewModel</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\Form</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Application\Model</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">ShareController</span> <span class="k">extends</span> <span class="nx">AbstractActionController</span>
<span class="p">{</span>
<span class="k">protected</span> <span class="nv">$acceptCriteria</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'Zend\View\Model\JsonModel'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'application/json'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">);</span>
<span class="k">protected</span> <span class="nv">$shareTable</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">addAction</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$viewModel</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">acceptableViewModelSelector</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">acceptCriteria</span><span class="p">);</span>
<span class="nv">$shareTable</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getShareTable</span><span class="p">();</span>
<span class="k">return</span> <span class="nv">$viewModel</span><span class="o">-></span><span class="na">setVariables</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'share'</span> <span class="o">=></span> <span class="nv">$shareTable</span><span class="o">-></span><span class="na">insert</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'user_id'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserAuthentication</span><span class="p">()</span><span class="o">-></span><span class="na">getIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getId</span><span class="p">(),</span>
<span class="s1">'author_id'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserAuthentication</span><span class="p">()</span><span class="o">-></span><span class="na">getIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getId</span><span class="p">(),</span>
<span class="s1">'author'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">zfcUserAuthentication</span><span class="p">()</span><span class="o">-></span><span class="na">getIdentity</span><span class="p">()</span><span class="o">-></span><span class="na">getEmail</span><span class="p">(),</span>
<span class="s1">'text'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">params</span><span class="p">()</span><span class="o">-></span><span class="na">fromPost</span><span class="p">(</span><span class="s1">'text'</span><span class="p">),</span>
<span class="s1">'type'</span> <span class="o">=></span> <span class="mi">1</span><span class="p">,</span>
<span class="p">)</span>
<span class="p">))</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">deleteAction</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$viewModel</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">acceptableViewModelSelector</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">acceptCriteria</span><span class="p">);</span>
<span class="k">return</span> <span class="nv">$viewModel</span><span class="o">-></span><span class="na">setVariables</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">'deleted'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">getShareTable</span><span class="p">()</span><span class="o">-></span><span class="na">delete</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'share_id'</span> <span class="o">=></span> <span class="nv">$this</span><span class="o">-></span><span class="na">params</span><span class="p">()</span><span class="o">-></span><span class="na">fromPost</span><span class="p">(</span><span class="s1">'share_id'</span><span class="p">))</span>
<span class="p">)</span>
<span class="p">)</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getShareTable</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$sm</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getServiceLocator</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span> <span class="o">=</span> <span class="nv">$sm</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'Application\Model\ShareTable'</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">shareTable</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Important part is <code>addAction</code> function. It just gets data sent by post and pass them to <code>ShareTable</code> model.
I've also used here context which are available in <em>ZF2</em>. Why? To be able to post something either using JavaScript or normal <strong>POST</strong> request. Thanks to it, again, I won't have to repeat myself.</p>
<p>It's important also to register our new controller inside of the module.config.php file by adding new invokable.</p>
<p>Look for <code>controllers</code> key and modify it</p>
<div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">return</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'controllers'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'invokables'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'Application\Controller\Index'</span> <span class="o">=></span> <span class="s1">'Application\Controller\IndexController'</span><span class="p">,</span>
<span class="s1">'Application\Controller\Share'</span> <span class="o">=></span> <span class="s1">'Application\Controller\ShareController'</span><span class="p">,</span>
<span class="p">),</span>
<span class="p">),</span>
<span class="p">);</span>
</pre></div>
<h3>Events, events, events</h3>
<p>It's time to make our share form working. To achieve it, we have to catch some user events. Let's come back to our <strong>AppView</strong> class. We'll extend it by adding some code inside.</p>
<div class="highlight"><pre><span class="c1">//application - manages input and send messages to collection</span>
<span class="kd">var</span> <span class="nx">AppView</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
<span class="nx">el</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#container'</span><span class="p">),</span>