Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / app_frameworks.html
blob1aa383ef1463e392f1ccf53af64339d72a9378c6
1 <h1>MVC Architecture</h1>
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&maxlabel=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>
14 <img src="{{static}}/images/jstransferrequests.png"
15 width="568"
16 height="292"
17 alt="JS transfer size and JS requests">
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 App, with its rich client-side features, is no exception.
26 </p>
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 problems in software design &mdash;
31 in our case &mdash; writing Chrome Apps.
32 We recommend that developers decouple the app
33 into a series of independent components following the MVC pattern.
34 </p>
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://emberjs.com/">ember.js</a>, <a href="http://angularjs.org/">AngularJS</a>, <a href="http://sencha.com/">Sencha</a>, <a href="http://www.kendoui.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>
44 <h2 id="mvc">MVC pattern overview</h2>
46 <p>
47 MVC offers architectural benefits over standard JavaScript &mdash;
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>
53 <p>
54 MVC is composed of three components:
55 </p>
57 <img src="{{static}}/images/mvc.png"
58 width="466"
59 height="303"
60 alt="model-view-controller">
62 <h3 id="model">Model</h3>
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 occurred.
68 </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.
72 </p>
74 <br>
76 <img src="{{static}}/images/todos.png"
77 width="444"
78 height="366"
79 alt="model-view-controller">
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>
88 <h3 id="view">View</h3>
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 App has access to the DOM.
94 </p>
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 controller’s job.
101 </p>
103 <h3 id="controller">Controller</h3>
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>
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 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">FileSystem API</a>.
122 </p>
125 There are a few variations of the MVC design pattern
126 such as MVP (Model&ndash;View&ndash;Presenter)
127 and MVVP(Model&ndash;View&ndash;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&ndash;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&ndash;of&ndash;concerns and
136 it's importance in writing modern web apps.
137 </p>
140 If you are interested in learning more,
141 we recommend <a href="https://plus.google.com/u/0/115133653231679625609/posts">Addy Osmani's</a> online book: <a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/">Learning JavaScript Design Patterns</a>.
142 </p>
145 To summarize, the MVC pattern brings modularity
146 to application developers and it enables:
147 </p>
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>
157 <h2 id="mvcpersistence">MVC persistence patterns</h2>
160 There are many different ways of implementing persistence
161 with an MVC framework, each with different trade&ndash;offs.
162 When writing Chrome Apps,
163 choose the frameworks with MVC and persistence patterns
164 that feel natural to you and fit you application needs.
165 </p>
167 <h3 id="model_persistence">Model does its own persistence - ActiveRecord pattern</h3>
170 Popular in both server&ndash;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>
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>
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 Apps provides both
200 <a href="storage">Chrome Storage API</a> and
201 <a href="fileSystem">HTML 5 fileSystem API</a> for client side storage.
202 </p>
204 <p>Pros:</p>
206 <ul>
207 <li>Simple to use and understand.</li>
208 </ul>
211 Cons:
212 </p>
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 difficult
217 (for example, FileSystem APIs vs indexedDB vs server&ndash;side).</li>
218 <li>Reusing Model in other applications may create conflicts,
219 such as sharing a single Customer class between two different views,
220 each view wanting to save to different places.</li>
221 </ul>
223 <h3 id="controller_persistence">Controller does persistence</h3>
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>
234 Pros:
235 </p>
237 <ul>
238 <li>Easier to test, controller can be passed a mock datastore to write tests against.</li>
239 <li>The same model can be reused with multiple datastores just by constructing controllers with different datastores.</li>
240 </ul>
243 Cons:
244 </p>
246 <ul>
247 <li>Code can be more complex to maintain.</li>
248 </ul>
250 <h3 id="app_controller">AppController does persistence</h3>
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>
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>
270 Pros:
271 </p>
273 <ul>
274 <li>Moves persistence layer even higher up the stack where it can be easily changed.</li>
275 <li>Doesn’t pollute lower level controllers like a DatePickerController with the need to know about persistence.</li>
276 </ul>
279 Cons:
280 </p>
282 <ul>
283 <li>Each ‘Page/Screen’ of the app now requires a lot of boilerplate to write or update: Model, View, Controller, AppController.</li>
284 </ul>
286 <h3 id="recommended">Recommended MVC frameworks</h3>
289 MVC is crucial to designing Chrome Apps.
290 We recommend the following <a href="contentSecurityPolicy">CSP&ndash;Compliant</a> MVC frameworks
291 for writing secure and scalable Chrome Apps:
292 </p>
294 <ul>
295 <li><a href="http://angularjs.org/">AngularJS</a>
296 (<a href="https://github.com/GoogleChrome/textdrive-app">Text Drive Reference App</a> and <a href="angular_framework">Build Apps with AngularJS tutorial</a>)</li>
297 <li><a href="http://www.kendoui.com/">Kendo UI</a>
298 (<a href="https://github.com/GoogleChrome/kendo-photo-booth-app">Photo Booth Reference App</a>)</li>
299 <li><a href="http://www.sencha.com/">Sencha</a>
300 (<a href="https://github.com/GoogleChrome/sencha-video-player-app">Video Player Reference App</a> and <a href="sencha_framework">Build Apps with Sencha Ext JS tutorial</a>)</li>
301 </ul>
303 <h2 id="resources">Useful resources</h2>
305 <h3 id="online">Online</h3>
307 <ul>
308 <li><a href="http://www.html5rocks.com/">HTML5Rocks.com</a></li>
309 <li><a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/">Learning JavaScript Design Patterns</a>
310 (by Addy Osmani)</li>
311 <li><a href="http://addyosmani.github.com/todomvc/">TodoMVC</a></li>
312 </ul>
314 <h3 id="books">Books</h3>
316 <ul>
317 <li><a href="http://www.amazon.com/JavaScript-Web-Applications-Alex-MacCaw/dp/144930351X">JavaScript Web Applications</a>
318 (By Alex MacCaw)</li>
319 <li><a href="http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752/ref=pd_sim_b_2">JavaScript Patterns</a>
320 (By Stoyan Stefonov)</li>
321 <li><a href="http://www.amazon.com/Maintainable-JavaScript-Nicholas-C-Zakas/dp/1449327680">Maintainable JavaScript</a>
322 (By Nicolas Z. Zakas)</li>
323 </ul>
325 <p class="backtotop"><a href="#top">Back to top</a></p>