OLD | NEW |
1 <h1>MVC Architecture</h1> | 1 <h1>MVC Architecture</h1> |
2 | 2 |
3 | 3 |
4 <p> | 4 <p> |
5 As modern browsers become more powerful with rich features, | 5 As modern browsers become more powerful with rich features, |
6 building full-blown web applications in JavaScript is not only feasible, | 6 building full-blown web applications in JavaScript is not only feasible, |
7 but increasingly popular. | 7 but increasingly popular. |
8 Based on | 8 Based on |
9 <a href="http://httparchive.org/trends.php?s=intersection&minlabel=Jan+20+2011&m
axlabel=Jan+15+2012">trends</a> | 9 <a href="http://httparchive.org/trends.php?s=intersection&minlabel=Jan+20+2011&m
axlabel=Jan+15+2012">trends</a> |
10 on <a href="http://httparchive.org/">HTTP Archive</a>, | 10 on <a href="http://httparchive.org/">HTTP Archive</a>, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 <p> | 53 <p> |
54 MVC is composed of three components: | 54 MVC is composed of three components: |
55 </p> | 55 </p> |
56 | 56 |
57 <img src="{{static}}/images/mvc.png" | 57 <img src="{{static}}/images/mvc.png" |
58 width="466" | 58 width="466" |
59 height="303" | 59 height="303" |
60 alt="model-view-controller"> | 60 alt="model-view-controller"> |
61 | 61 |
62 <h3>Model</h3> | 62 <h3 id="model">Model</h3> |
63 | 63 |
64 <p> | 64 <p> |
65 Model is where the application’s data objects are stored. | 65 Model is where the application’s data objects are stored. |
66 The model doesn’t know anything about views and controllers. | 66 The model doesn’t know anything about views and controllers. |
67 When a model changes, typically it will notify its observers that a change has o
ccurred. | 67 When a model changes, typically it will notify its observers that a change has o
ccurred. |
68 </p> | 68 </p> |
69 | 69 |
70 <p> | 70 <p> |
71 To understand this further, let’s use the Todo list app, a simple, one page web
app that tracks your task list. | 71 To understand this further, let’s use the Todo list app, a simple, one page web
app that tracks your task list. |
72 </p> | 72 </p> |
73 | 73 |
74 <br> | 74 <br> |
75 | 75 |
76 <img src="{{static}}/images/todos.png" | 76 <img src="{{static}}/images/todos.png" |
77 width="444" | 77 width="444" |
78 height="366" | 78 height="366" |
79 alt="model-view-controller"> | 79 alt="model-view-controller"> |
80 | 80 |
81 <p> | 81 <p> |
82 The model here represents attributes associated | 82 The model here represents attributes associated |
83 with each todo item such as description and status. | 83 with each todo item such as description and status. |
84 When a new todo item is created, | 84 When a new todo item is created, |
85 it is stored in an instance of the model. | 85 it is stored in an instance of the model. |
86 </p> | 86 </p> |
87 | 87 |
88 <h3>View</h3> | 88 <h3 id="view">View</h3> |
89 | 89 |
90 <p> | 90 <p> |
91 View is what's presented to the users and how users interact with the app. | 91 View is what's presented to the users and how users interact with the app. |
92 The view is made with HTML, CSS, JavaScript and often templates. | 92 The view is made with HTML, CSS, JavaScript and often templates. |
93 This part of your Chrome packaged app has access to the DOM. | 93 This part of your Chrome packaged app has access to the DOM. |
94 </p> | 94 </p> |
95 | 95 |
96 <p> | 96 <p> |
97 For example, in the above todo list web app, | 97 For example, in the above todo list web app, |
98 you can create a view that nicely presents the list of todo items to your users. | 98 you can create a view that nicely presents the list of todo items to your users. |
99 Users can also enter a new todo item through some input format; | 99 Users can also enter a new todo item through some input format; |
100 however, the view doesn’t know how to update the model because that’s the contro
ller’s job. | 100 however, the view doesn’t know how to update the model because that’s the contro
ller’s job. |
101 </p> | 101 </p> |
102 | 102 |
103 <h3>Controller</h3> | 103 <h3 id="controller">Controller</h3> |
104 | 104 |
105 <p> | 105 <p> |
106 The controller is the decision maker and the glue between the model and view. | 106 The controller is the decision maker and the glue between the model and view. |
107 The controller updates the view when the model changes. | 107 The controller updates the view when the model changes. |
108 It also adds event listeners to the view and | 108 It also adds event listeners to the view and |
109 updates the model when the user manipulates the view. | 109 updates the model when the user manipulates the view. |
110 </p> | 110 </p> |
111 | 111 |
112 <p> | 112 <p> |
113 In the todo list web app, | 113 In the todo list web app, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 <h2 id="mvcpersistence">MVC persistence patterns</h2> | 157 <h2 id="mvcpersistence">MVC persistence patterns</h2> |
158 | 158 |
159 <p> | 159 <p> |
160 There are many different ways of implementing persistence | 160 There are many different ways of implementing persistence |
161 with an MVC framework, each with different trade–offs. | 161 with an MVC framework, each with different trade–offs. |
162 When writing Chrome packaged apps, | 162 When writing Chrome packaged apps, |
163 choose the frameworks with MVC and persistence patterns | 163 choose the frameworks with MVC and persistence patterns |
164 that feel natural to you and fit you application needs. | 164 that feel natural to you and fit you application needs. |
165 </p> | 165 </p> |
166 | 166 |
167 <h3>Model does its own persistence - ActiveRecord pattern</h3> | 167 <h3 id="model_persistence">Model does its own persistence - ActiveRecord pattern
</h3> |
168 | 168 |
169 <p> | 169 <p> |
170 Popular in both server–side frameworks like Ruby on Rails, | 170 Popular in both server–side frameworks like Ruby on Rails, |
171 and client-side frameworks like | 171 and client-side frameworks like |
172 <a href="http://backbonejs.org">Backbone.js</a> and | 172 <a href="http://backbonejs.org">Backbone.js</a> and |
173 <a href="http://emberjs.com/">ember.js</a>, | 173 <a href="http://emberjs.com/">ember.js</a>, |
174 the ActiveRecord pattern places the responsibility | 174 the ActiveRecord pattern places the responsibility |
175 for persistence on the model itself | 175 for persistence on the model itself |
176 and is typically implemented via JSON API. | 176 and is typically implemented via JSON API. |
177 </p> | 177 </p> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 213 |
214 <ul> | 214 <ul> |
215 <li>Hard to test since the persistence layer is ‘baked’ into the object
hierarchy.</li> | 215 <li>Hard to test since the persistence layer is ‘baked’ into the object
hierarchy.</li> |
216 <li>Having different objects use different persistent stores is difficul
t | 216 <li>Having different objects use different persistent stores is difficul
t |
217 (for example, FileSystem APIs vs indexedDB vs server–side)
.</li> | 217 (for example, FileSystem APIs vs indexedDB vs server–side)
.</li> |
218 <li>Reusing Model in other applications may create conflicts, | 218 <li>Reusing Model in other applications may create conflicts, |
219 such as sharing a single Customer class between two different vi
ews, | 219 such as sharing a single Customer class between two different vi
ews, |
220 each view wanting to save to different places.</li> | 220 each view wanting to save to different places.</li> |
221 </ul> | 221 </ul> |
222 | 222 |
223 <h3>Controller does persistence</h3> | 223 <h3 id="controller_persistence">Controller does persistence</h3> |
224 | 224 |
225 <p> | 225 <p> |
226 In this pattern, the controller holds a reference | 226 In this pattern, the controller holds a reference |
227 to both the model and a datastore | 227 to both the model and a datastore |
228 and is responsible for keeping the model persisted. | 228 and is responsible for keeping the model persisted. |
229 The controller responds to lifecycle events like Load, Save, Delete, | 229 The controller responds to lifecycle events like Load, Save, Delete, |
230 and issues commands to the datastore to fetch or update the model. | 230 and issues commands to the datastore to fetch or update the model. |
231 </p> | 231 </p> |
232 | 232 |
233 <p> | 233 <p> |
234 Pros: | 234 Pros: |
235 </p> | 235 </p> |
236 | 236 |
237 <ul> | 237 <ul> |
238 <li>Easier to test, controller can be passed a mock datastore to write t
ests against.</li> | 238 <li>Easier to test, controller can be passed a mock datastore to write t
ests against.</li> |
239 <li>The same model can be reused with multiple datastores just by constr
ucting controllers with different datastores.</li> | 239 <li>The same model can be reused with multiple datastores just by constr
ucting controllers with different datastores.</li> |
240 </ul> | 240 </ul> |
241 | 241 |
242 <p> | 242 <p> |
243 Cons: | 243 Cons: |
244 </p> | 244 </p> |
245 | 245 |
246 <ul> | 246 <ul> |
247 <li>Code can be more complex to maintain.</li> | 247 <li>Code can be more complex to maintain.</li> |
248 </ul> | 248 </ul> |
249 | 249 |
250 <h3>AppController does persistence</h3> | 250 <h3 id="app_controller">AppController does persistence</h3> |
251 | 251 |
252 <p> | 252 <p> |
253 In some patterns, there is a supervising controller responsible | 253 In some patterns, there is a supervising controller responsible |
254 for navigating between one MVC and another. | 254 for navigating between one MVC and another. |
255 The AppController decides, for example, | 255 The AppController decides, for example, |
256 that a ‘Back’ button moves the client from an editing screen | 256 that a ‘Back’ button moves the client from an editing screen |
257 (which contains MVC widgets/formats), | 257 (which contains MVC widgets/formats), |
258 to a settings screen. | 258 to a settings screen. |
259 </p> | 259 </p> |
260 | 260 |
(...skipping 19 matching lines...) Expand all Loading... |
280 </ul> | 280 </ul> |
281 | 281 |
282 <p> | 282 <p> |
283 Cons: | 283 Cons: |
284 </p> | 284 </p> |
285 | 285 |
286 <ul> | 286 <ul> |
287 <li>Each ‘Page/Screen’ of the app now requires a lot of boilerplate to w
rite or update: Model, View, Controller, AppController.</li> | 287 <li>Each ‘Page/Screen’ of the app now requires a lot of boilerplate to w
rite or update: Model, View, Controller, AppController.</li> |
288 </ul> | 288 </ul> |
289 | 289 |
290 <h3>Recommended MVC frameworks</h3> | 290 <h3 id="recommended">Recommended MVC frameworks</h3> |
291 | 291 |
292 <p> | 292 <p> |
293 MVC is crucial to designing Chrome packaged apps. | 293 MVC is crucial to designing Chrome packaged apps. |
294 We recommend the following <a href="app_csp.html">CSP–Compliant</a> MVC fr
ameworks | 294 We recommend the following <a href="app_csp.html">CSP–Compliant</a> MVC fr
ameworks |
295 for writing secure and scalable Chrome packaged apps: | 295 for writing secure and scalable Chrome packaged apps: |
296 </p> | 296 </p> |
297 | 297 |
298 <ul> | 298 <ul> |
299 <li><a href="http://angularjs.org/">AngularJS</a> | 299 <li><a href="http://angularjs.org/">AngularJS</a> |
300 (<a href="https://github.com/GoogleChrome/textdrive-app">Text Dr
ive Reference App</a>)</li> | 300 (<a href="https://github.com/GoogleChrome/textdrive-app">Text Dr
ive Reference App</a>)</li> |
301 <li><a href="http://kendo.com/">Kendo UI</a> | 301 <li><a href="http://kendo.com/">Kendo UI</a> |
302 (<a href="https://github.com/GoogleChrome/kendo-photo-booth-app"
>Photo Booth Reference App</a>)</li> | 302 (<a href="https://github.com/GoogleChrome/kendo-photo-booth-app"
>Photo Booth Reference App</a>)</li> |
303 <li><a href="http://www.sencha.com/">Sencha</a> | 303 <li><a href="http://www.sencha.com/">Sencha</a> |
304 (<a href="https://github.com/GoogleChrome/sencha-video-player-ap
p">Video Player Reference App</a>)</li> | 304 (<a href="https://github.com/GoogleChrome/sencha-video-player-ap
p">Video Player Reference App</a>)</li> |
305 </ul> | 305 </ul> |
306 | 306 |
307 <h2 id="resources">Useful resources</h2> | 307 <h2 id="resources">Useful resources</h2> |
308 | 308 |
309 <h3>Online</h3> | 309 <h3 id="online">Online</h3> |
310 | 310 |
311 <ul> | 311 <ul> |
312 <li><a href="http://www.html5rocks.com/">HTML5Rocks.com</a></li> | 312 <li><a href="http://www.html5rocks.com/">HTML5Rocks.com</a></li> |
313 <li><a href="http://addyosmani.com/resources/essentialjsdesignpatterns/b
ook/">Learning JavaScript Design Patterns</a> | 313 <li><a href="http://addyosmani.com/resources/essentialjsdesignpatterns/b
ook/">Learning JavaScript Design Patterns</a> |
314 (by Addy Osmani)</li> | 314 (by Addy Osmani)</li> |
315 <li><a href="http://addyosmani.github.com/todomvc/">TodoMVC</a></li> | 315 <li><a href="http://addyosmani.github.com/todomvc/">TodoMVC</a></li> |
316 </ul> | 316 </ul> |
317 | 317 |
318 <h3>Books</h3> | 318 <h3 id="books">Books</h3> |
319 | 319 |
320 <ul> | 320 <ul> |
321 <li><a href="http://www.amazon.com/JavaScript-Web-Applications-Alex-MacC
aw/dp/144930351X">JavaScript Web Applications</a> | 321 <li><a href="http://www.amazon.com/JavaScript-Web-Applications-Alex-MacC
aw/dp/144930351X">JavaScript Web Applications</a> |
322 (By Alex MacCaw)</li> | 322 (By Alex MacCaw)</li> |
323 <li><a href="http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/d
p/0596806752/ref=pd_sim_b_2">JavaScript Patterns</a> | 323 <li><a href="http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/d
p/0596806752/ref=pd_sim_b_2">JavaScript Patterns</a> |
324 (By Stoyan Stefonov)</li> | 324 (By Stoyan Stefonov)</li> |
325 <li><a href="http://www.amazon.com/Maintainable-JavaScript-Nicholas-C-Za
kas/dp/1449327680">Maintainable JavaScript</a> | 325 <li><a href="http://www.amazon.com/Maintainable-JavaScript-Nicholas-C-Za
kas/dp/1449327680">Maintainable JavaScript</a> |
326 (By Nicolas Z. Zakas)</li> | 326 (By Nicolas Z. Zakas)</li> |
327 </ul> | 327 </ul> |
328 | 328 |
329 <p class="backtotop"><a href="#top">Back to top</a></p> | 329 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |