OLD | NEW |
---|---|
(Empty) | |
1 <h1 id="lab_3_model_view_controller">Lab 3 - Model-View-Controller</h1> | |
2 | |
3 <p>Whenever your application grows beyond a single script with a few dozen lines , it gets | |
4 harder and harder to manage without a good separation of roles among app compone nts. One of the most common | |
5 models for structuring a complex application, no matter what language, is the Mo del-View-Controller (MVC) and | |
6 its variants, like Model-View-Presentation (MVP).</p> | |
7 | |
8 <p>There are several frameworks to help apply <a href="http://developer.chrome.c om/trunk/apps/app_frameworks.html">MVC concepts</a> to a Javascript application, and most of them, | |
9 as long as they are CSP compliant, can be used in a Chrome App. We will use the <a href="http://angularjs.org/">AngularJS</a> framework in parts of this tutoria l, with a special focus on the framework in this section.</p> | |
10 | |
11 <h2 id="you_should_also_read">You should also read</h2> | |
12 | |
13 <ul> | |
14 <li><p><a href="http://developer.chrome.com/apps/angular_framework.html">Build A pps with AngularJS</a> tutorial</p></li> | |
mkearney1
2013/02/12 22:22:11
This link is broken. You probably just need #angul
Renato Mangini (chromium)
2013/02/13 22:55:33
Done.
| |
15 <li><p><a href="http://angularjs.org/">AngularJS Todo</a> tutorial</p></li> | |
16 </ul> | |
17 | |
18 <p class="note"><b>Note:</b> Chrome apps don't enforce any specific framewo rk or programming style. This section and additional parts of this tutorial use the AngularJS framework. Most of the code from this section was copied, with sma ll changes, from the AngularJS Todo tutorial. </p> | |
19 | |
20 <h2 id="create_a_simple_view_using_angularjs">Create a simple view using Angular JS</h2> | |
21 | |
22 <ol> | |
23 <li><p>Download the <a href="http://ajax.googleapis.com/ajax/libs/angularjs/1.0. 2/angular.min.js">Angular script</a> and save it as <a href="https://github.com/ GoogleChrome/chrome-app-codelab/blob/master/lab3_mvc/angularjs/simpleview/angula r.min.js">angular.min.js</a>.</p></li> | |
24 <li><p>Change your <a href="https://github.com/GoogleChrome/chrome-app-codelab/b lob/master/lab3_mvc/angularjs/simpleview/index.html">index.html</a> to use a sim ple Angular sample: | |
25 <pre> | |
26 <html ng-app ng-csp> | |
27 <head> | |
28 <script src="angular.min.js"></script> | |
29 <link rel="stylesheet" href="todo.css"> | |
30 </head> | |
31 <body> | |
32 <h2>Todo</h2> | |
33 <div> | |
34 <ul> | |
35 <li> | |
36 {{todoText}} | |
37 </li> | |
38 </ul> | |
39 <input type="text" ng-model="todoText" size=" 30" | |
40 placeholder="type your todo here"> | |
41 </div> | |
42 </body> | |
43 </html> | |
44 </pre></p></li> | |
45 <li><p>Add a simple stylesheet: <a href="https://github.com/GoogleChrome/chrome- app-codelab/blob/master/lab3_mvc/angularjs/simpleview/todo.css">todo.css</a> | |
46 <pre> | |
47 body { | |
48 font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; | |
49 } | |
50 | |
51 ul { | |
52 list-style: none; | |
53 } | |
54 | |
55 button, input[type=submit] { | |
56 background-color: #0074CC; | |
57 background-image: linear-gradient(top, #08C, #05C); | |
58 border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); | |
59 text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); | |
60 color: white; | |
61 } | |
62 | |
63 .done-true { | |
64 text-decoration: line-through; | |
65 color: grey; | |
66 } | |
67 </pre></li><li>Check the results by reloading the app: open the app, right-click and select Reload App.</li> | |
68 </ol> | |
69 | |
70 <p class="note"><b>Note:</b> The ng-csp directive tells Angular to run in a &qu ot;content security mode". You don't need this directive when using Ang ular v1.1.0+. We've included it here so that the sample works regardless of the Angular version in use.</p> | |
71 | |
72 <h2 id="add_a_controller">Add a Controller</h2> | |
73 | |
74 The previous sample, although interesting, is not exactly useful. Let's tran sform it into a real Todo list, instead of a simple Todo item. We will create a controller (controller.js) and make some small changes in the index.html: | |
75 | |
76 <ol> | |
77 <li>Add the <a href="https://github.com/GoogleChrome/chrome-app-codelab/blob/mas ter/lab3_mvc/angularjs/withcontroller/controller.js">controller.js</a> file: | |
78 <pre> | |
79 function TodoCtrl($scope) { | |
80 $scope.todos = [ | |
81 {text:'learn angular', done:true}, | |
82 {text:'build an angular Chrome packaged app', done:false}]; | |
83 | |
84 $scope.addTodo = function() { | |
85 $scope.todos.push({text:$scope.todoText, done:false}); | |
86 $scope.todoText = ''; | |
87 }; | |
88 | |
89 $scope.remaining = function() { | |
90 var count = 0; | |
91 angular.forEach($scope.todos, function(todo) { | |
92 count += todo.done ? 0 : 1; | |
93 }); | |
94 return count; | |
95 }; | |
96 | |
97 $scope.archive = function() { | |
98 var oldTodos = $scope.todos; | |
99 $scope.todos = []; | |
100 angular.forEach(oldTodos, function(todo) { | |
101 if (!todo.done) $scope.todos.push(todo); | |
102 }); | |
103 }; | |
104 } | |
105 </pre></p></li><li><p>Change <a href="https://github.com/GoogleChrome/chrome-app -codelab/blob/master/lab3_mvc/angularjs/withcontroller/index.html">index.html</a > file: | |
106 <pre> | |
107 <html ng-app ng-csp> | |
108 <head> | |
109 <script src="angular.min.js"></script> | |
110 <script src="controller.js"></script> | |
111 <link rel="stylesheet" href="todo.css"> | |
112 </head> | |
113 <body> | |
114 <h2>Todo</h2> | |
115 <div ng-controller="TodoCtrl"> | |
116 <span>{{remaining()}} of {{todos.lengt h}} remaining</span> | |
117 [ <a href="" ng-click="archive()">archive</a& gt; ] | |
118 <ul> | |
119 <li ng-repeat="todo in todos"> | |
120 <input type="checkbox" ng-model="todo.done"> | |
121 <span class="done-{{todo.done}}">& #123;{todo.text}}</span> | |
122 </li> | |
123 </ul> | |
124 <form ng-submit="addTodo()"> | |
125 <input type="text" ng-model="todoText" size=" ;30" | |
126 placeholder="add new todo here"> | |
127 <input class="btn-primary" type="submit" value=&q uot;add"> | |
128 </form> | |
129 </div> | |
130 </body> | |
131 </html> | |
132 </pre></p></li> | |
133 <li><p>Check the results by reloading the app: open the app, right-click and sel ect Reload App.</p></li> | |
134 </ol> | |
135 | |
136 <p>Note how the data, stored in an array inside the controller, binds to the vie w and is automatically updated when it is changed by the controller.</p> | |
137 | |
138 <h1 id="takeaways_">Takeaways:</h1> | |
139 | |
140 <ul> | |
141 <li><p>Chrome apps are offline first, so the recommended way to include third-pa rty scripts is to download and package them inside your app.</p></li> | |
mkearney1
2013/02/12 22:22:11
Make "offline first" a link to #offline_apps.html.
Renato Mangini (chromium)
2013/02/13 22:55:33
Done.
| |
142 <li><p>You can use any framework you want, as long as it complies with Content S ecurity Policies and other restrictions that Chrome apps are enforced to follow. </p></li> | |
143 <li><p>MVC frameworks make your life easier. Use them, specially if you want to build a non-trivial application.</p></li> | |
144 </ul> | |
145 | |
146 <h1 id="what_39_s_next_">What's next?</h1> | |
147 | |
148 <p>Eventually in <a href="app_codelab4_testing.html">lab4_testing</a>, you will test your app. | |
149 Right now this lab is a work-in-progress. <a href="https://docs.google.com/a/goo gle.com/forms/d/1x3309vpp-KTiHqZWOCQhjVrIWxkm0wEBp2IWMG2ywbU/viewform?id=1x3309v pp-KTiHqZWOCQhjVrIWxkm0wEBp2IWMG2ywbU">Please let us know what you need to test your apps</a>. | |
mkearney1
2013/02/12 22:22:11
Remove sentence on feedback. You might want to als
Renato Mangini (chromium)
2013/02/13 22:55:33
Done.
| |
150 You can then skip ahead to <a href="app_codelab5_data.html">lab5_data</a>.</p> | |
OLD | NEW |