OLD | NEW |
---|---|
1 <h1 id="lab_3_model_view_controller">Create MVC</h1> | 1 <h1 id="lab_3_model_view_controller">Create MVC</h1> |
2 | 2 |
3 <p>Whenever your application grows beyond a single script with a few dozen lines , | 3 <p>Whenever your application grows beyond a single script with a few dozen lines , |
4 it gets harder and harder to manage without a good separation | 4 it gets harder and harder to manage without a good separation |
5 of roles among app components. | 5 of roles among app components. |
6 One of the most common models for structuring a complex application, | 6 One of the most common models for structuring a complex application, |
7 no matter what language, | 7 no matter what language, |
8 is the Model-View-Controller (MVC) and its variants, | 8 is the Model-View-Controller (MVC) and its variants, |
9 like Model-View-Presentation (MVP).</p> | 9 like Model-View-Presentation (MVP).</p> |
10 | 10 |
11 <p>There are several frameworks to help apply | 11 <p>There are several frameworks to help apply |
12 <a href="app_frameworks.html">MVC concepts</a> | 12 <a href="app_frameworks">MVC concepts</a> |
13 to a Javascript application, and most of them, | 13 to a Javascript application, and most of them, |
14 as long as they are CSP compliant, can be used in a Chrome App. | 14 as long as they are CSP compliant, can be used in a Chrome App. |
15 In this lab, | 15 In this lab, |
16 we'll add an MVC model using both pure JavaScript and | 16 we'll add an MVC model using both pure JavaScript and |
17 the <a href="http://angularjs.org/">AngularJS</a> framework. | 17 the <a href="http://angularjs.org/">AngularJS</a> framework. |
18 Most of the AngularJS code from this section was copied, | 18 Most of the AngularJS code from this section was copied, |
19 with small changes, from the AngularJS Todo tutorial.</p> | 19 with small changes, from the AngularJS Todo tutorial.</p> |
20 | 20 |
21 <p class="note"><b>Note:</b> | 21 <p class="note"><b>Note:</b> |
22 Chrome Apps don't enforce any specific framework or programming style. | 22 Chrome Apps don't enforce any specific framework or programming style. |
23 </p> | 23 </p> |
24 | 24 |
25 <h2 id="simple">Create a simple view</h2> | 25 <h2 id="simple">Create a simple view</h2> |
26 | 26 |
27 <h3 id="basic-mvc">Add MVC basics</h3> | 27 <h3 id="basic-mvc">Add MVC basics</h3> |
28 | 28 |
29 <p>If using AngularJS, download the | 29 <p>If using AngularJS, download the |
30 <a href="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js">An gular script</a> | 30 <a href="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js">An gular script</a> |
31 and save it as | 31 and save it as |
32 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /angularjs/simpleview/angular.min.js">angular.min.js</a>.</p> | 32 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /angularjs/simpleview/angular.min.js">angular.min.js</a>.</p> |
33 | 33 |
34 <p>If using JavaScript, | 34 <p>If using JavaScript, |
35 you will need to add a very simple controller with basic MVC functionalities: | 35 you will need to add a very simple controller with basic MVC functionalities: |
36 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/simpleview/controller.js">JavaScript controller.js</a></p> | 36 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/simpleview/controller.js">JavaScript controller.js</a></p> |
37 | 37 |
38 <h3 id="update-view">Update view</h3> | 38 <h3 id="update-view">Update view</h3> |
39 | 39 |
40 <p>Change the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/m aster/lab3_mvc/angularjs/simpleview/index.html">AngularJS index.html</a> or | 40 <p>Change the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/m aster/lab3_mvc/angularjs/simpleview/index">AngularJS index</a> or |
mkearney1
2014/04/09 19:43:29
Need to make sure links work to GitHub samples wit
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename for a s
| |
41 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/simpleview/index.html">JavaScript index.html</a> to use a simple sam ple: | 41 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/simpleview/index">JavaScript index</a> to use a simple sample: |
mkearney1
2014/04/09 19:43:29
Need to make sure links work to GitHub samples wit
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename for a s
| |
42 </p> | 42 </p> |
43 | 43 |
44 <tabs data-group="source"> | 44 <tabs data-group="source"> |
45 | 45 |
46 <header tabindex="0" data-value="angular">Angular</header> | 46 <header tabindex="0" data-value="angular">Angular</header> |
47 <header tabindex="0" data-value="js">JavaScript</header> | 47 <header tabindex="0" data-value="js">JavaScript</header> |
48 | 48 |
49 <content> | 49 <content> |
50 <pre data-filename="index.html"> | 50 <pre data-filename="index"> |
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename for a s
| |
51 <!doctype html> | 51 <!doctype html> |
52 <html ng-app ng-csp> | 52 <html ng-app ng-csp> |
53 <head> | 53 <head> |
54 <script src="angular.min.js"></script> | 54 <script src="angular.min.js"></script> |
55 <link rel="stylesheet" href="todo.css"> | 55 <link rel="stylesheet" href="todo.css"> |
56 </head> | 56 </head> |
57 <body> | 57 <body> |
58 <h2>Todo</h2> | 58 <h2>Todo</h2> |
59 <div> | 59 <div> |
60 <ul> | 60 <ul> |
61 <li> | 61 <li> |
62 {{todoText}} | 62 {{todoText}} |
63 </li> | 63 </li> |
64 </ul> | 64 </ul> |
65 <input type="text" ng-model="todoText" size="30" | 65 <input type="text" ng-model="todoText" size="30" |
66 placeholder="type your todo here"> | 66 placeholder="type your todo here"> |
67 </div> | 67 </div> |
68 </body> | 68 </body> |
69 </html> | 69 </html> |
70 </pre> | 70 </pre> |
71 </content> | 71 </content> |
72 <content> | 72 <content> |
73 <pre data-filename="index.html"> | 73 <pre data-filename="index"> |
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename for a s
| |
74 <!doctype html> | 74 <!doctype html> |
75 <html> | 75 <html> |
76 <head> | 76 <head> |
77 <link rel="stylesheet" href="todo.css"> | 77 <link rel="stylesheet" href="todo.css"> |
78 </head> | 78 </head> |
79 <body> | 79 <body> |
80 <h2>Todo</h2> | 80 <h2>Todo</h2> |
81 <div> | 81 <div> |
82 <ul> | 82 <ul> |
83 <li id="todoText"> | 83 <li id="todoText"> |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 | 312 |
313 updateCounters(model); | 313 updateCounters(model); |
314 | 314 |
315 }); | 315 }); |
316 </pre> | 316 </pre> |
317 </content> | 317 </content> |
318 </tabs> | 318 </tabs> |
319 | 319 |
320 <h3 id="index">Update view</h3> | 320 <h3 id="index">Update view</h3> |
321 | 321 |
322 <p>Change the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/m aster/lab3_mvc/angularjs/withcontroller/index.html">AngularJS index.html</a> or | 322 <p>Change the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/m aster/lab3_mvc/angularjs/withcontroller/index">AngularJS index</a> or |
mkearney1
2014/04/09 19:43:29
Test that link works without .html to Github repo
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename in a sa
| |
323 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/withcontroller/index.html">JavaScript index.html</a>: | 323 <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc /javascript/withcontroller/index">JavaScript index</a>: |
mkearney1
2014/04/09 19:43:29
Again, test link works without .html to Github rep
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename in a sa
| |
324 </p> | 324 </p> |
325 | 325 |
326 <tabs data-group="source"> | 326 <tabs data-group="source"> |
327 | 327 |
328 <header tabindex="0" data-value="angular">Angular</header> | 328 <header tabindex="0" data-value="angular">Angular</header> |
329 <header tabindex="0" data-value="js">JavaScript</header> | 329 <header tabindex="0" data-value="js">JavaScript</header> |
330 | 330 |
331 <content> | 331 <content> |
332 <pre data-filename="index.html"> | 332 <pre data-filename="index"> |
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename in a sa
| |
333 <html ng-app ng-csp> | 333 <html ng-app ng-csp> |
334 <head> | 334 <head> |
335 <script src="angular.min.js"></script> | 335 <script src="angular.min.js"></script> |
336 <script src="controller.js"></script> | 336 <script src="controller.js"></script> |
337 <link rel="stylesheet" href="todo.css"> | 337 <link rel="stylesheet" href="todo.css"> |
338 </head> | 338 </head> |
339 <body> | 339 <body> |
340 <h2>Todo</h2> | 340 <h2>Todo</h2> |
341 <div ng-controller="TodoCtrl"> | 341 <div ng-controller="TodoCtrl"> |
342 <span>{{remaining()}} of {{todos.lengt h}} remaining</span> | 342 <span>{{remaining()}} of {{todos.lengt h}} remaining</span> |
343 [ <a href="" ng-click="archive()">archive</a& gt; ] | 343 [ <a href="" ng-click="archive()">archive</a& gt; ] |
344 <ul> | 344 <ul> |
345 <li ng-repeat="todo in todos"> | 345 <li ng-repeat="todo in todos"> |
346 <input type="checkbox" ng-model="todo.done"> | 346 <input type="checkbox" ng-model="todo.done"> |
347 <span class="done-{{todo.done}}">& #123;{todo.text}}</span> | 347 <span class="done-{{todo.done}}">& #123;{todo.text}}</span> |
348 </li> | 348 </li> |
349 </ul> | 349 </ul> |
350 <form ng-submit="addTodo()"> | 350 <form ng-submit="addTodo()"> |
351 <input type="text" ng-model="todoText" size=" ;30" | 351 <input type="text" ng-model="todoText" size=" ;30" |
352 placeholder="add new todo here"> | 352 placeholder="add new todo here"> |
353 <input class="btn-primary" type="submit" value=&q uot;add"> | 353 <input class="btn-primary" type="submit" value=&q uot;add"> |
354 </form> | 354 </form> |
355 </div> | 355 </div> |
356 </body> | 356 </body> |
357 </html> | 357 </html> |
358 </pre> | 358 </pre> |
359 </content> | 359 </content> |
360 <content> | 360 <content> |
361 <pre data-filename="index.html"> | 361 <pre data-filename="index"> |
mkearney1
2014/04/09 19:43:29
Keep .html here as it refers to a filename in a sa
| |
362 <!doctype html> | 362 <!doctype html> |
363 <html> | 363 <html> |
364 <head> | 364 <head> |
365 <link rel="stylesheet" href="todo.css"> | 365 <link rel="stylesheet" href="todo.css"> |
366 </head> | 366 </head> |
367 <body> | 367 <body> |
368 <h2>Todo</h2> | 368 <h2>Todo</h2> |
369 <div> | 369 <div> |
370 <span><span id="remaining"></span> of <span id="length"></span> remaining</span> | 370 <span><span id="remaining"></span> of <span id="length"></span> remaining</span> |
371 [ <a href="" id="archive">archive</a> ] | 371 [ <a href="" id="archive">archive</a> ] |
(...skipping 26 matching lines...) Expand all Loading... | |
398 <h3 id="check2">Check the results</h3> | 398 <h3 id="check2">Check the results</h3> |
399 | 399 |
400 <p> | 400 <p> |
401 Check the results by reloading the app: open the app, right-click and select Rel oad App.</li> | 401 Check the results by reloading the app: open the app, right-click and select Rel oad App.</li> |
402 </p> | 402 </p> |
403 | 403 |
404 <h2 id="takeaways_">Takeaways</h2> | 404 <h2 id="takeaways_">Takeaways</h2> |
405 | 405 |
406 <ul> | 406 <ul> |
407 <li><p>Chrome Apps are | 407 <li><p>Chrome Apps are |
408 <a href="offline_apps.html">offline first</a>, | 408 <a href="offline_apps">offline first</a>, |
409 so the recommended way to include third-party scripts is to download | 409 so the recommended way to include third-party scripts is to download |
410 and package them inside your app.</p></li> | 410 and package them inside your app.</p></li> |
411 <li><p>You can use any framework you want, | 411 <li><p>You can use any framework you want, |
412 as long as it complies with Content Security Policies | 412 as long as it complies with Content Security Policies |
413 and other restrictions that Chrome Apps are enforced to follow.</p></li> | 413 and other restrictions that Chrome Apps are enforced to follow.</p></li> |
414 <li><p>MVC frameworks make your life easier. | 414 <li><p>MVC frameworks make your life easier. |
415 Use them, specially if you want to build a non-trivial application.</p></li> | 415 Use them, specially if you want to build a non-trivial application.</p></li> |
416 </ul> | 416 </ul> |
417 | 417 |
418 <h2 id="you_should_also_read">You should also read</h2> | 418 <h2 id="you_should_also_read">You should also read</h2> |
419 | 419 |
420 <ul> | 420 <ul> |
421 <li><p><a href="angular_framework.html">Build Apps with AngularJS</a> tutorial</ p></li> | 421 <li><p><a href="angular_framework">Build Apps with AngularJS</a> tutorial</p></l i> |
422 <li><p><a href="http://angularjs.org/">AngularJS Todo</a> tutorial</p></li> | 422 <li><p><a href="http://angularjs.org/">AngularJS Todo</a> tutorial</p></li> |
423 </ul> | 423 </ul> |
424 | 424 |
425 <h2 id="what_39_s_next_">What's next?</h2> | 425 <h2 id="what_39_s_next_">What's next?</h2> |
426 | 426 |
427 <p>In <a href="app_codelab5_data.html">4 - Save and Fetch Data</a>, | 427 <p>In <a href="app_codelab5_data">4 - Save and Fetch Data</a>, |
428 you will modify your Todo list app so that Todo items are saved.</p> | 428 you will modify your Todo list app so that Todo items are saved.</p> |
OLD | NEW |