OLD | NEW |
(Empty) | |
| 1 <h1>MVC Architecture</h1> |
| 2 |
| 3 |
| 4 <p> |
| 5 As modern browsers become more powerful with rich features, |
| 6 building full-blown web applications in JavaScript is not only feasible, |
| 7 but increasingly popular. |
| 8 Based on |
| 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>, |
| 11 deployed JavaScript code size has grown 45% over the course of the year. |
| 12 </p> |
| 13 |
| 14 <img src="{{static}}/images/jstransferrequests.png" |
| 15 width="568" |
| 16 height="292" |
| 17 alt="JS transfer size and JS requests"> |
| 18 |
| 19 <p> |
| 20 With JavaScript's popularity climbing, |
| 21 our client-side applications are much more complex than before. |
| 22 Application development requires collaboration from multiple developers. |
| 23 Writing <strong>maintainable</strong> and |
| 24 <strong>reusable</strong> code is crucial in the new web app era. |
| 25 The Chrome packaged app, with its rich client-side features, is no exception. |
| 26 </p> |
| 27 |
| 28 <p> |
| 29 Design patterns are important to write maintainable and reusable code. |
| 30 A pattern is a reusable solution that can be applied to commonly occurring probl
ems in software design — |
| 31 in our case — writing Chrome packaged apps. |
| 32 We recommend that developers decouple the app |
| 33 into a series of independent components following the MVC pattern. |
| 34 </p> |
| 35 |
| 36 <p> |
| 37 In the last few years, |
| 38 a series of JavaScript MVC frameworks have been developed, |
| 39 such as <a href="http://backbonejs.org/">backbone.js</a>, <a href="http://emberj
s.com/">ember.js</a>, <a href="http://angularjs.org/">AngularJS</a>, <a href="ht
tp://sencha.com/">Sencha</a>, <a href="http://kendo.com/">Kendo UI</a>, and more
. |
| 40 While they all have their unique advantages, each one of them follows some form
of MVC pattern |
| 41 with the goal of encouraging developers to write more structured JavaScript code
. |
| 42 </p> |
| 43 |
| 44 <h2 id="mvc">MVC pattern overview</h2> |
| 45 |
| 46 <p> |
| 47 MVC offers architectural benefits over standard JavaScript — |
| 48 it helps you write better organized, and therefore more maintainable code. |
| 49 This pattern has been used and extensively tested |
| 50 over multiple languages and generations of programmers. |
| 51 </p> |
| 52 |
| 53 <p> |
| 54 MVC is composed of three components: |
| 55 </p> |
| 56 |
| 57 <img src="{{static}}/images/mvc.png" |
| 58 width="466" |
| 59 height="303" |
| 60 alt="model-view-controller"> |
| 61 |
| 62 <h3>Model</h3> |
| 63 |
| 64 <p> |
| 65 Model is where the application’s data objects are stored. |
| 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. |
| 68 </p> |
| 69 |
| 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. |
| 72 </p> |
| 73 |
| 74 <br> |
| 75 |
| 76 <img src="{{static}}/images/todos.png" |
| 77 width="444" |
| 78 height="366" |
| 79 alt="model-view-controller"> |
| 80 |
| 81 <p> |
| 82 The model here represents attributes associated |
| 83 with each todo item such as description and status. |
| 84 When a new todo item is created, |
| 85 it is stored in an instance of the model. |
| 86 </p> |
| 87 |
| 88 <h3>View</h3> |
| 89 |
| 90 <p> |
| 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. |
| 93 This part of your Chrome packaged app has access to the DOM. |
| 94 </p> |
| 95 |
| 96 <p> |
| 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. |
| 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. |
| 101 </p> |
| 102 |
| 103 <h3>Controller</h3> |
| 104 |
| 105 <p> |
| 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. |
| 108 It also adds event listeners to the view and |
| 109 updates the model when the user manipulates the view. |
| 110 </p> |
| 111 |
| 112 <p> |
| 113 In the todo list web app, |
| 114 when the user checks an item as completed, |
| 115 the click is forwarded to the controller. |
| 116 The controller modifies the model to mark item as completed. |
| 117 If the data needs to be persistent, it also makes an async save to the server. |
| 118 In rich client-side web app development such as Chrome packaged apps, |
| 119 keeping the data persistent in local storage is also crucial. |
| 120 In this case, the controller also handles saving the data |
| 121 to the client-side storage such as <a href="app_storage.html">FileSystem API</a>
. |
| 122 </p> |
| 123 |
| 124 <p> |
| 125 There are a few variations of the MVC design pattern |
| 126 such as MVP (Model–View–Presenter) |
| 127 and MVVP(Model–View–ViewModel). |
| 128 Even with the so called MVC design pattern itself, |
| 129 there is some variation between the traditional MVC pattern |
| 130 vs the modern interpretation in various programming languages. |
| 131 For example, some MVC–based frameworks will have |
| 132 the view observe the changes in the models |
| 133 while others will let the controller handle the view update. |
| 134 This article is not focused on the comparison of various implementations |
| 135 but rather on the separation–of–concerns and |
| 136 it's importance in writing modern web apps. |
| 137 </p> |
| 138 |
| 139 <p> |
| 140 If you are interested in learning more, |
| 141 we recommend <a href="https://plus.google.com/u/0/115133653231679625609/posts">A
ddy Osmani's</a> online book: <a href="http://addyosmani.com/resources/essential
jsdesignpatterns/book/">Learning JavaScript Design Patterns</a>. |
| 142 </p> |
| 143 |
| 144 <p> |
| 145 To summarize, the MVC pattern brings modularity |
| 146 to application developers and it enables: |
| 147 </p> |
| 148 |
| 149 <ul> |
| 150 <li>Reusable and extendable code.</li> |
| 151 <li>Separation of view logic from business logic.</li> |
| 152 <li>Allow simultaneous work between developers who are responsible |
| 153 for different components (such as UI layer and core logic).</li> |
| 154 <li>Easier to maintain.</li> |
| 155 </ul> |
| 156 |
| 157 <h2 id="mvcpersistence">MVC persistence patterns</h2> |
| 158 |
| 159 <p> |
| 160 There are many different ways of implementing persistence |
| 161 with an MVC framework, each with different trade–offs. |
| 162 When writing Chrome packaged apps, |
| 163 choose the frameworks with MVC and persistence patterns |
| 164 that feel natural to you and fit you application needs. |
| 165 </p> |
| 166 |
| 167 <h3>Model does its own persistence - ActiveRecord pattern</h3> |
| 168 |
| 169 <p> |
| 170 Popular in both server–side frameworks like Ruby on Rails, |
| 171 and client-side frameworks like |
| 172 <a href="http://backbonejs.org">Backbone.js</a> and |
| 173 <a href="http://emberjs.com/">ember.js</a>, |
| 174 the ActiveRecord pattern places the responsibility |
| 175 for persistence on the model itself |
| 176 and is typically implemented via JSON API. |
| 177 </p> |
| 178 |
| 179 <p> |
| 180 A slightly different take from |
| 181 having a model handle the persistence |
| 182 is to introduce a separate concept of Store and Adapter API. |
| 183 Store, Model and |
| 184 Adapter (in some frameworks it is called Proxy) |
| 185 work hand by hand. |
| 186 Store is the repository that holds the loaded models, |
| 187 and it also provides functions such as creating, |
| 188 querying and filtering the model instances contained within it. |
| 189 </p> |
| 190 |
| 191 <p> |
| 192 An adapter, or a proxy, receives the requests from a store and |
| 193 translates them into appropriate actions to take |
| 194 against your persistent data layer |
| 195 (such as JSON API). |
| 196 This is interesting in the modern web app design |
| 197 because you often interact with more than one persistent data layer |
| 198 such as a remote server and browser’s local storage. |
| 199 Chrome package apps provides both |
| 200 <a href="storage.html">Chrome Storage API</a> and |
| 201 <a href="fileSystem.html">HTML 5 fileSystem API</a> for client side storage. |
| 202 </p> |
| 203 |
| 204 <p>Pros:</p> |
| 205 |
| 206 <ul> |
| 207 <li>Simple to use and understand.</li> |
| 208 </ul> |
| 209 |
| 210 <p> |
| 211 Cons: |
| 212 </p> |
| 213 |
| 214 <ul> |
| 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 |
| 217 (for example, FileSystem APIs vs indexedDB vs server–side)
.</li> |
| 218 <li>Reusing Model in other applications may create conflicts, |
| 219 such as sharing a single Customer class between two different vi
ews, |
| 220 each view wanting to save to different places.</li> |
| 221 </ul> |
| 222 |
| 223 <h3>Controller does persistence</h3> |
| 224 |
| 225 <p> |
| 226 In this pattern, the controller holds a reference |
| 227 to both the model and a datastore |
| 228 and is responsible for keeping the model persisted. |
| 229 The controller responds to lifecycle events like Load, Save, Delete, |
| 230 and issues commands to the datastore to fetch or update the model. |
| 231 </p> |
| 232 |
| 233 <p> |
| 234 Pros: |
| 235 </p> |
| 236 |
| 237 <ul> |
| 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> |
| 240 </ul> |
| 241 |
| 242 <p> |
| 243 Cons: |
| 244 </p> |
| 245 |
| 246 <ul> |
| 247 <li>Code can be more complex to maintain.</li> |
| 248 </ul> |
| 249 |
| 250 <h3>AppController does persistence</h3> |
| 251 |
| 252 <p> |
| 253 In some patterns, there is a supervising controller responsible |
| 254 for navigating between one MVC and another. |
| 255 The AppController decides, for example, |
| 256 that a ‘Back’ button moves the client from an editing screen |
| 257 (which contains MVC widgets/formats), |
| 258 to a settings screen. |
| 259 </p> |
| 260 |
| 261 <p> |
| 262 In the AppController pattern, |
| 263 the AppController responds to events |
| 264 and changes the app’s current screen by issuing a call |
| 265 to the datastore to load any models needed and |
| 266 constructing all of the matching views and controllers for that screen. |
| 267 </p> |
| 268 |
| 269 <p> |
| 270 Pros: |
| 271 </p> |
| 272 |
| 273 <ul> |
| 274 <li>Moves persistence layer even higher up the stack where it can be eas
ily changed.</li> |
| 275 <li>Doesn’t pollute lower level controllers like a DatePickerController
with the need to know about persistence.</li> |
| 276 <li>Aligns nicely with an ‘Intent’ model. |
| 277 Each AppController corresponds to an intent, like “ChoosePhoto”,
or “SendMessage”. |
| 278 The Intent contains a reference to the model data needed (Custom
er#123) and |
| 279 the type of CRUD operation (load, save, delete, and so on).</li> |
| 280 </ul> |
| 281 |
| 282 <p> |
| 283 Cons: |
| 284 </p> |
| 285 |
| 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> |
| 288 </ul> |
| 289 |
| 290 <h3>Recommended MVC frameworks</h3> |
| 291 |
| 292 <p> |
| 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 |
| 295 for writing secure and scalable Chrome packaged apps: |
| 296 </p> |
| 297 |
| 298 <ul> |
| 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> |
| 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> |
| 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> |
| 305 </ul> |
| 306 |
| 307 <h2 id="resources">Useful resources</h2> |
| 308 |
| 309 <h3>Online</h3> |
| 310 |
| 311 <ul> |
| 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> |
| 314 (by Addy Osmani)</li> |
| 315 <li><a href="http://addyosmani.github.com/todomvc/">TodoMVC</a></li> |
| 316 </ul> |
| 317 |
| 318 <h3>Books</h3> |
| 319 |
| 320 <ul> |
| 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> |
| 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> |
| 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> |
| 327 </ul> |
| 328 |
| 329 <p class="backtotop"><a href="#top">Back to top</a></p> |
OLD | NEW |