1 <h1 id=
"import-existing-app">
2 <span class=
"h1-step">Step
2:
</span>
3 Import an Existing Web App
7 <strong>Want to start fresh from here?
</strong>
8 Find the previous step's code in the
<a href=
"https://github.com/mangini/io13-codelab/archive/master.zip">reference code zip
</a> under
<strong><em>cheat_code
> solution_for_step1
</strong></em>.
11 <p>In this step, you will learn:
</p>
14 <li>How to adapt an existing web application for the Chrome Apps platform.
</li>
15 <li>How to make your app scripts Content Security Policy (CSP) compliant.
</li>
16 <li>How to implement local storage using the
<a href=
"/apps/storage" title=
"Read 'chrome.storage.local' in the Chrome developer docs">chrome.storage.local
</a>.
</li>
20 <em>Estimated time to complete this step:
20 minutes.
</em>
22 To preview what you will complete in this step,
<a href=
"#launch">jump down to the bottom of this page
↓</a>.
25 <h2 id=
"todomvc">Import an existing Todo app
</h2>
27 <p>As a starting point, import the
<a href=
"http://todomvc.com/vanilla-examples/vanillajs/">vanilla
28 JavaScript version
</a> of
<a href=
"http://todomvc.com/">TodoMVC
</a>, a common benchmark app, into your project.
</p>
30 <p>We've included a version of the TodoMVC app in the
31 <a href=
"https://github.com/mangini/io13-codelab/archive/master.zip">reference code zip
</a> in the
<strong><em>todomvc
</em></strong> folder.
32 Copy all files (including folders) from
<em>todomvc
</em> into your project folder.
</p>
35 <img src=
"{{static}}/images/app_codelab/copy-todomvc.png" alt=
"Copy todomvc folder into codelab folder">
38 <p>You will be asked to replace
<em>index.html
</em>. Go ahead and accept.
</p>
41 <img src=
"{{static}}/images/app_codelab/replace-index.png" alt=
"Replace index.html"><br>
44 <p>You should now have the following file structure in your application folder:
</p>
47 <img src=
"{{static}}/images/app_codelab/todomvc-copied.png" alt=
"New project folder">
48 <figcaption>The files highlighted in blue are from the
<em>todomvc
</em> folder.
</figcaption>
51 <li><strong>background.js</strong> (from step 1)</li>
52 <li><strong>bower_components/</strong> (from todomvc)</li>
53 <li><strong>bower.json</strong> (from todomvc)</li>
54 <li><strong>icon_128.png</strong> (from step 1)</li>
55 <li><strong>index.html</strong> (from todomvc)</li>
56 <li><strong>js/</strong> (from todomvc)</li>
57 <li><strong>manifest.json</strong> (from step 1)</li>
63 <p>Reload your app now (
<b>right-click
> Reload App
</b>). You should see the basic UI but you won't be able to add todos.
</p>
65 <h2 id=
"csp-compliance">Make scripts Content Security Policy (CSP) compliant
</h2>
67 <p>Open the DevTools Console (
<strong>right-click
> Inspect Element
</strong>, then select the
<strong>Console
</strong> tab). You will see an error about refusing to execute an inline script:
</p>
70 <img src=
"{{static}}/images/app_codelab/csp-console-error.png" alt=
"Todo app with CSP console log error">
73 > Refused to execute inline script because it violates the following Content <br>
74 > Security Policy directive: "default-src 'self' chrome-extension-resource:". <br>
75 > Note that 'script-src' was not explicitly set, so 'default-src' is used as a <br>
82 <p>Let's fix this error by making the app
<a href=
"/apps/contentSecurityPolicy">Content Security Policy
</a> compliant.
83 One of the most common CSP non-compliances is caused by inline JavaScript. Examples of inline JavaScript include event
84 handlers as DOM attributes (e.g.
<code><button onclick=''
></code>) and
<code><script
></code> tags with
85 content inside the HTML.
</p>
87 <p>The solution is simple: move the inline content to a new file.
</p>
89 <p>1. Near the bottom of
<strong><em>index.html
</em></strong>, remove the inline
90 JavaScript and instead include
<em>js/bootstrap.js
</em>:
</p>
92 <pre data-filename=
"index.html">
93 <script
src=
"bower_components/director/build/director.js"></script
>
94 <strike><script
></strike>
95 <strike> // Bootstrap app data
</strike>
96 <strike> window.app = {};
</strike>
97 <strike></script
></strike>
98 <b><script
src=
"js/bootstrap.js"></script
></b>
99 <script
src=
"js/helpers.js"></script
>
100 <script
src=
"js/store.js"></script
>
103 <p>2. Create a file in the
<strong><em>js
</em></strong> folder named
<strong><em>bootstrap.js
</em></strong>. Move the previously inline code to be in this file:
</p>
105 <pre data-filename=
"bootstrap.js">
106 // Bootstrap app data
110 <p>You'll still have a non-working Todo app if you reload the app now but you're getting closer.
</p>
112 <h2 id=
"convert-storage">Convert localStorage to chrome.storage.local
</h2>
114 <p>If you open the DevTools Console now, the previous error should be gone. There is a new error, however, about
<code>window.localStorage
</code> not being available:
</p>
117 <img src=
"{{static}}/images/app_codelab/localStorage-console-error.png" alt=
"Todo app with localStorage console log error">
120 > Uncaught window.localStorage is not available in packaged apps. Use <br>
121 > chrome.storage.local instead. store.js:21
126 <p>Chrome Apps do not support
127 <a href=
"http://dev.w3.org/html5/webstorage/#the-localstorage-attribute"><code>localStorage
</code></a>
128 as
<code>localStorage
</code> is synchronous.
129 Synchronous access to blocking resources (I/O) in a single-threaded runtime
130 could make your app unresponsive.
</p>
132 <p>Chrome Apps have an equivalent API that can store objects asynchronously.
133 This will help avoid the sometimes costly object-
>string-
>object serialization process.
</p>
135 <p>To address the error message in our app, you need to convert
<code>localStorage
</code> to
136 <a href=
"/apps/storage" title=
"Read 'chrome.storage.local' in the Chrome developer docs">chrome.storage.local
</a>.
</p>
138 <h3 id=
"update-permissions">Update app permissions
</h3>
140 <p>In order to use
<code>chrome.storage.local
</code>, you need to request the
<code>storage
</code> permission. In
<strong><em>manifest.json
</em></strong>, add
<code>"storage"</code> to the
<code>permissions
</code> array:
</p>
142 <pre data-filename=
"manifest.json">
143 "permissions": [
<b>"storage"</b>],
146 <h3 id=
"get-and-set">Learn about local.storage.set() and local.storage.get()
</h3>
148 <p>To save and retrieve todo items, you need to know about the
<code>set()
</code> and
<code>get()
</code> methods of the
<code>chrome.storage
</code> API.
</p>
150 <p>The
<a href=
"/apps/storage#method-StorageArea-set" title=
"Read 'chrome.storage.local.set()' in the Chrome developer docs">set()
</a>
151 method accepts an object of key-value pairs as its first parameter. An optional callback function is the second parameter. For example:
</p>
154 chrome.storage.local.set({secretMessage:'Psst!',timeSet:Date.now()}, function() {
155 console.log(
"Secret message saved");
159 <p>The
<a href=
"/apps/storage#method-StorageArea-get" title=
"Read 'chrome.storage.local.get()' in the Chrome developer docs">get()
</a> method accepts an optional first parameter for the datastore keys you wish to retreive. A single key can be passed as a string; multiple keys can be arranged into an array of strings or a dictionary object.
</p>
161 <p>The second parameter, which is required, is a callback function. In the returned object, use the keys requested in the first parameter to access the stored values. For example:
</p>
164 chrome.storage.local.get(['secretMessage','timeSet'], function(data) {
165 console.log(
"The secret message:", data.secretMessage,
"saved at:", data.timeSet);
169 <p>If you want to
<code>get()
</code> everything that is currently in
<code>chrome.storage.local
</code>,
170 omit the first parameter:
</p>
173 chrome.storage.local.get(function(data) {
178 <p>Unlike
<code>localStorage
</code>, you won't be able to inspect locally stored items using the DevTools Resources panel. You can, however, interact with
<code>chrome.storage
</code> from the JavaScript Console like so:
</p>
181 <img src=
"{{static}}/images/app_codelab/get-set-in-console.png" alt=
"Use the Console to debug chrome.storage">
184 <h3 id=
"preview-changes">Preview required API changes
</h3>
186 <p>Most of the remaining steps in converting the Todo app are small changes
187 to the API calls. Changing all the places where
<code>localStorage
</code>
188 is currently being used, though time-consuming and error-prone, is required.
</p>
191 To maximize your fun with this codelab, it'll be best if you overwrite your
192 <strong><em>store.js
</em></strong>,
<strong><em>controller.js
</em></strong>, and
<strong><em>model.js
</em></strong>
193 with the ones from
<strong><em>cheat_code/solution_for_step_2
</em></strong> in the reference code zip.
195 Once you've done that, continue reading as we'll go over each of the changes individually.
198 <p>The key differences between
<code>localStorage
</code> and
<code>chrome.storage
</code> come from the async nature of
<code>chrome.storage
</code>:
</p>
202 Instead of writing to
<code>localStorage
</code> using simple assignment, you need to use
<code>chrome.storage.local.set()
</code> with optional callbacks.
204 var data = { todos: [] };
205 localStorage[dbName] = JSON.stringify(data);
210 storage[dbName] = { todos: [] };
211 chrome.storage.local.set( storage, function() {
217 Instead of accessing
<code>localStorage[myStorageName]
</code> directly, you need to use
<code>chrome.storage.local.get(myStorageName,function(storage){...})
</code> and then parse the returned
<code>storage
</code> object in the callback function.
219 var todos = JSON.parse(localStorage[dbName]).todos;
223 chrome.storage.local.get(dbName, function(storage) {
224 var todos = storage[dbName].todos;
229 The function
<code>.bind(this)
</code> is used on all callbacks to ensure
<code>this
</code> refers to the
<code>this
</code> of the
<code>Store
</code> prototype. (More info on bound functions can be found on the MDN docs:
<a href=
"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind">Function.prototype.bind()
</a>.)
232 this.scope = 'inside Store';
233 chrome.storage.local.set( {}, function() {
234 console.log(this.scope); // outputs: 'undefined'
242 this.scope = 'inside Store';
243 chrome.storage.local.set( {}, function() {
244 console.log(this.scope); // outputs: 'inside Store'
245 }
<b>.bind(this)
</b>);
252 <p>Keep these key differences in mind as we cover retrieving, saving, and removing todo items in the following sections.
</p>
254 <h3 id=
"retrieve-items">Retrieve todo items
</h3>
256 Let's update the Todo app in order to retrieve todo items:
258 <p>1. The
<code>Store
</code> constructor method takes care of initializing the Todo app with all the existing todo items from the datastore.
259 The method first checks if the datastore exists.
260 If it doesn't, it'll create an empty array of
<code>todos
</code> and save it to the datastore so there are no runtime read errors.
</p>
262 <p>In
<strong><em>js/store.js
</em></strong>, convert the use of
<code>localStorage
</code> in the constructor method to instead use
263 <code>chrome.storage.local
</code>:
</p>
265 <pre data-filename=
"store.js">
266 function Store(name, callback) {
270 callback = callback || function () {};
272 dbName = this._dbName = name;
274 <strike>if (!localStorage[dbName]) {
</strike>
275 <strike> data = {
</strike>
276 <strike> todos: []
</strike>
278 <strike> localStorage[dbName] = JSON.stringify(data);
</strike>
280 <strike>callback.call(this, JSON.parse(localStorage[dbName]));
</strike>
282 <b>chrome.storage.local.get(dbName, function(storage) {
</b>
283 <b> if ( dbName in storage ) {
</b>
284 <b> callback.call(this, storage[dbName].todos);
</b>
286 <b> storage = {};
</b>
287 <b> storage[dbName] = { todos: [] };
</b>
288 <b> chrome.storage.local.set( storage, function() {
</b>
289 <b> callback.call(this, storage[dbName].todos);
</b>
290 <b> }.bind(this));
</b>
292 <b>}.bind(this));
</b>
296 <p>2. The
<code>find()
</code> method is used when reading todos from the Model. The returned results change based on whether you are filtering by
"All",
"Active", or
"Completed".
</p>
298 <p>Convert
<code>find()
</code> to use
<code>chrome.storage.local
</code>:
</p>
300 <pre data-filename=
"store.js">
301 Store.prototype.find = function (query, callback) {
306 <strike>var todos = JSON.parse(localStorage[this._dbName]).todos;
</strike>
308 <strike>callback.call(this, todos.filter(function (todo) {
</strike>
309 <b>chrome.storage.local.get(this._dbName, function(storage) {
</b>
310 <b> var todos = storage[this._dbName].todos.filter(function (todo) {
</b>
311 <b> </b>for (var q in query) {
312 <b> </b> return query[q] === todo[q];
315 <b> callback.call(this, todos);
</b>
316 <b>}.bind(this));
</b>
317 <strike>}));
</strike>
321 <p>3. Similiar to
<code>find()
</code>,
<code>findAll()
</code> gets all todos from the Model. Convert
<code>findAll()
</code> to use
<code>chrome.storage.local
</code>:
</p>
323 <pre data-filename=
"store.js">
324 Store.prototype.findAll = function (callback) {
325 callback = callback || function () {};
326 <strike>callback.call(this, JSON.parse(localStorage[this._dbName]).todos);
</strike>
327 <b>chrome.storage.local.get(this._dbName, function(storage) {
</b>
328 <b> var todos = storage[this._dbName] && storage[this._dbName].todos || [];
</b>
329 <b> callback.call(this, todos);
</b>
330 <b>}.bind(this));
</b>
334 <h3 id=
"save-items">Save todos items
</h3>
336 <p>The current
<code>save()
</code> method presents a challenge. It depends on two async
337 operations (get and set) that operate on the whole monolithic JSON storage
338 every time. Any batch updates on more than one todo item, like
"mark all todos as
339 completed", will result in a data hazard known as
340 <a href=
"http://en.wikipedia.org/wiki/Hazard_(computer_architecture)#Read_After_Write_.28RAW.29">Read-After-Write
</a>.
341 This issue wouldn't happen if we were using a more appropriate data storage,
342 like IndexedDB, but we are trying to minimize the conversion effort for this
345 <p>There are several ways to fix it so we will use this opportunity to slightly
346 refactor
<code>save()
</code> by taking an array of todo IDs to be updated all at once:
</p>
348 <p>1. To start off, wrap everything already inside
<code>save()
</code>
349 with a
<code>chrome.storage.local.get()
</code> callback:
</p>
351 <pre data-filename=
"store.js">
352 Store.prototype.save = function (id, updateData, callback) {
353 <b>chrome.storage.local.get(this._dbName, function(storage) {
</b>
354 <b> </b>var data = JSON.parse(localStorage[this._dbName]);
356 <b> </b>if (typeof id !== 'object') {
361 <b>}.bind(this));
</b>
365 <p>2. Convert all the
<code>localStorage
</code> instances with
<code>chrome.storage.local
</code>:
</p>
367 <pre data-filename=
"store.js">
368 Store.prototype.save = function (id, updateData, callback) {
369 chrome.storage.local.get(this._dbName, function(storage) {
370 <strike>var data = JSON.parse(localStorage[this._dbName]);
</strike>
371 <b>var data = storage[this._dbName];
</b>
372 var todos = data.todos;
374 callback = callback || function () {};
376 // If an ID was actually given, find the item and update each property
377 if ( typeof id !== 'object' ) {
380 <strike>localStorage[this._dbName] = JSON.stringify(data);
</strike>
381 <strike>callback.call(this, JSON.parse(localStorage[this._dbName]).todos);
</strike>
382 <b>chrome.storage.local.set(storage, function() {
</b>
383 <b> chrome.storage.local.get(this._dbName, function(storage) {
</b>
384 <b> callback.call(this, storage[this._dbName].todos);
</b>
385 <b> }.bind(this));
</b>
386 <b>}.bind(this));
</b>
388 callback = updateData;
393 updateData.id = new Date().getTime();
395 <strike>localStorage[this._dbName] = JSON.stringify(data);
</strike>
396 <strike>callback.call(this, [updateData]);
</strike>
397 <b>chrome.storage.local.set(storage, function() {
</b>
398 <b> callback.call(this, [updateData]);
</b>
399 <b>}.bind(this));
</b>
405 <p>3. Then update the logic to operate on an array instead of a single item:
</p>
407 <pre data-filename=
"store.js">
408 Store.prototype.save = function (id, updateData, callback) {
409 chrome.storage.local.get(this._dbName, function(storage) {
410 var data = storage[this._dbName];
411 var todos = data.todos;
413 callback = callback || function () {};
415 // If an ID was actually given, find the item and update each property
416 if ( typeof id !== 'object'
<b>|| Array.isArray(id)
</b> ) {
417 <b>var ids = [].concat( id );
</b>
418 <b>ids.forEach(function(id) {
</b>
419 for (var i =
0; i
< todos.length; i++) {
420 if (todos[i].id == id) {
421 for (var x in updateData) {
422 todos[i][x] = updateData[x];
428 chrome.storage.local.set(storage, function() {
429 chrome.storage.local.get(this._dbName, function(storage) {
430 callback.call(this, storage[this._dbName].todos);
434 callback = updateData;
439 updateData.id = new Date().getTime();
441 <b>todos.push(updateData);
</b>
442 chrome.storage.local.set(storage, function() {
443 callback.call(this, [updateData]);
450 <h3 id=
"complete-items">Mark todo items as complete
</h3>
452 <p>Now that app is operating on arrays, you need to change how the app handles a user clicking on the
<b>Clear completed (#)
</b> button:
</p>
454 <p>1. In
<strong><em>controller.js
</em></strong>, update
<code>toggleAll()
</code> to call
<code>toggleComplete()
</code>
455 only once with an array of todos instead of marking a todo as completed
456 one by one. Also delete the call to
<code>_filter()
</code> since you'll be adjusting
457 the
<code>toggleComplete
</code> <code>_filter()
</code>.
</p>
459 <pre data-filename=
"controller.js">
460 Controller.prototype.toggleAll = function (e) {
461 var completed = e.target.checked ?
1 :
0;
463 if (completed ===
0) {
466 this.model.read({ completed: query }, function (data) {
468 data.forEach(function (item) {
469 <strike>this.toggleComplete(item.id, e.target, true);
</strike>
470 <b>ids.push(item.id);
</b>
472 <b>this.toggleComplete(ids, e.target, false);
</b>
475 <strike>this._filter();
</strike>
479 <p>2. Now update
<code>toggleComplete()
</code> to accept both a single todo or an array of todos. This includes moving
<code>filter()
</code> to be inside the
<code>update()
</code>, instead of outside.
</p>
481 <pre data-filename=
"controller.js">
482 Controller.prototype.toggleComplete = function (
<strike>id
</strike> <b>ids
</b>, checkbox, silent) {
483 var completed = checkbox.checked ?
1 :
0;
484 this.model.update(
<strike>id
</strike> <b>ids
</b>, { completed: completed }, function () {
485 <b>if ( ids.constructor != Array ) {
</b>
486 <b> ids = [ ids ];
</b>
488 <b>ids.forEach( function(id) {
</b>
489 var listItem = $$('[
data-id=
"' + id + '"]');
495 listItem.className = completed ? 'completed' : '';
497 // In case it was toggled from an event and not by clicking the checkbox
498 listItem.querySelector('input').checked = completed;
501 <b>if (!silent) {
</b>
502 <b> this._filter();
</b>
505 }
<b>.bind(this)
</b>);
507 <strike>if (!silent) {
</strike>
508 <strike> this._filter();
</strike>
513 <h3 id=
"count-items">Count todo items
</h3>
515 <p>After switching to async storage, there is a minor bug that shows up when getting the number of todos. You'll need to wrap the count operation in a callback function:
</p>
517 <p>1. In
<strong><em>model.js
</em></strong>, update
<code>getCount()
</code> to accept a callback:
</p>
519 <pre data-filename=
"model.js">
520 Model.prototype.getCount = function (
<b>callback
</b>) {
526 this.storage.findAll(function (data) {
527 data.each(function (todo) {
528 if (todo.completed ===
1) {
535 <b>if (callback) callback(todos);
</b>
537 <strike>return todos;
</strike>
541 <p>2. Back in
<strong><em>controller.js
</em></strong>, update
<code>_updateCount()
</code> to use
542 the async
<code>getCount()
</code> you edited in the previous step:
</p>
544 <pre data-filename=
"controller.js">
545 Controller.prototype._updateCount = function () {
546 <strike>var todos = this.model.getCount();
</strike>
547 <b>this.model.getCount(function(todos) {
</b>
548 <b> </b>this.$todoItemCounter.innerHTML = this.view.itemCounter(todos.active);
550 <b> </b>this.$clearCompleted.innerHTML = this.view.clearCompletedButton(todos.completed);
551 <b> </b>this.$clearCompleted.style.display = todos.completed
> 0 ? 'block' : 'none';
553 <b> </b>this.$toggleAll.checked = todos.completed === todos.total;
555 <b> </b>this._toggleFrame(todos);
556 <b>}.bind(this));
</b>
561 <p>You are almost there! If you reload the app now, you will be able to insert new
562 todos without any console errors.
</p>
564 <h3 id=
"remove-items">Remove todos items
</h3>
566 <p>Now that the app can save todo items, you're close to being done!
567 You still get errors when you attempt to
<em>remove
</em> todo items:
</p>
570 <img src=
"{{static}}/images/app_codelab/remove-todo-console-error.png" alt=
"Todo app with localStorage console log error">
573 <p>1. In
<strong><em>store.js
</em></strong>, convert all the
<code>localStorage
</code> instances to use
<code>chrome.storage.local
</code>:
</p>
575 <p>a) To start off, wrap everything already inside
<code>remove()
</code> with a
<code>get()
</code> callback:
</p>
577 <pre data-filename=
"store.js">
578 Store.prototype.remove = function (id, callback) {
579 <b>chrome.storage.local.get(this._dbName, function(storage) {
</b>
580 <b> </b>var data = JSON.parse(localStorage[this._dbName]);
581 <b> </b>var todos = data.todos;
583 <b> </b>for (var i =
0; i < todos.length; i++) {
584 <b> </b> if (todos[i].id == id) {
585 <b> </b> todos.splice(i,
1);
590 <b> </b>localStorage[this._dbName] = JSON.stringify(data);
591 <b> </b>callback.call(this, JSON.parse(localStorage[this._dbName]).todos);
592 <b>}.bind(this));
</b>
596 <p>b) Then convert the contents within the
<code>get()
</code> callback:
</p>
598 <pre data-filename=
"store.js">
599 Store.prototype.remove = function (id, callback) {
600 chrome.storage.local.get(this._dbName, function(storage) {
601 <strike>var data = JSON.parse(localStorage[this._dbName]);
</strike>
602 <b>var data = storage[this._dbName];
</b>
603 var todos = data.todos;
605 for (var i =
0; i
< todos.length; i++) {
606 if (todos[i].id == id) {
612 <strike>localStorage[this._dbName] = JSON.stringify(data);
</strike>
613 <strike>callback.call(this, JSON.parse(localStorage[this._dbName]).todos);
</strike>
614 <b>chrome.storage.local.set(storage, function() {
</b>
615 <b> callback.call(this, todos);
</b>
616 <b>}.bind(this));
</b>
621 <p>2. The same Read-After-Write data hazard issue previously present in the
622 <code>save()
</code> method is also present when removing items so you will need
623 to update a few more places to allow for batch operations on a list of todo IDs.
</p>
625 <p>a) Still in
<em>store.js
</em>, update
<code>remove()
</code>:
</p>
627 <pre data-filename=
"store.js">
628 Store.prototype.remove = function (id, callback) {
629 chrome.storage.local.get(this._dbName, function(storage) {
630 var data = storage[this._dbName];
631 var todos = data.todos;
633 <b>var ids = [].concat(id);
</b>
634 <b>ids.forEach( function(id) {
</b>
635 <b> </b>for (var i =
0; i
< todos.length; i++) {
636 <b> </b> if (todos[i].id == id) {
637 <b> </b> todos.splice(i,
1);
643 chrome.storage.local.set(storage, function() {
644 callback.call(this, todos);
650 <p>b) In
<strong><em>controller.js
</em></strong>, change
<code>removeCompletedItems()
</code> to
651 make it call
<code>removeItem()
</code> on all IDs at once:
</p>
653 <pre data-filename=
"controller.js">
654 Controller.prototype.removeCompletedItems = function () {
655 this.model.read({ completed:
1 }, function (data) {
657 data.forEach(function (item) {
658 <strike>this.removeItem(item.id);
</strike>
659 <b>ids.push(item.id);
</b>
661 <b>this.removeItem(ids);
</b>
668 <p>c) Finally, still in
<em>controller.js
</em>, change the
<code>removeItem()
</code> to support
669 removing multiple items from the DOM at once, and move the
<code>_filter()
</code> call to be inside the callback:
</p>
671 <pre data-filename=
"controller.js">
672 Controller.prototype.removeItem = function (id) {
673 this.model.remove(id, function () {
674 <b>var ids = [].concat(id);
</b>
675 <b>ids.forEach( function(id) {
</b>
676 <b> </b>this.$todoList.removeChild($$('[
data-id=
"' + id + '"]'));
677 <b>}.bind(this));
</b>
678 <b>this._filter();
</b>
680 <strike>this._filter();
</strike>
684 <h3 id=
"drop-items">Drop all todo items
</h3>
686 <p>There is one more method in
<em>store.js
</em> using
<code>localStorage
</code>:
</p>
688 <pre data-filename=
"store.js">
689 Store.prototype.drop = function (callback) {
690 localStorage[this._dbName] = JSON.stringify({todos: []});
691 callback.call(this, JSON.parse(localStorage[this._dbName]).todos);
695 <p>This method is not being called in the current app so, if you want an extra challenge, try implementing it on your own.
696 Hint: Have a look at
<code><a href=
"/apps/storage#method-StorageArea-remove">chrome.storage.local.clear()
</a></code>.
</p>
698 <h2 id=
"launch">Launch your finished Todo app
</h2>
700 <p>You are done Step
2! Reload your app and you should now have
701 a fully working Chrome packaged version of TodoMVC.
</p>
704 <img src=
"{{static}}/images/app_codelab/step2-completed.gif" alt=
"The finished Todo app after Step 2">
708 <strong>Troubleshooting
</strong>
710 Remember to always check the DevTools Console to see if there are any error messages.
713 <h2 id=
"recap">For more information
</h2>
715 <p>For more detailed information about some of the APIs introduced in this step, refer to:
</p>
719 <a href=
"/apps/contentSecurityPolicy" title=
"Read 'Content Security Policy' in the Chrome developer docs">Content Security Policy
</a>
720 <a href=
"#csp-compliance" class=
"anchor-link-icon" title=
"This feature mentioned in 'Make scripts Content Security Policy (CSP) compliant'">↑</a>
723 <a href=
"/apps/declare_permissions" title=
"Read 'Declare Permissions' in the Chrome developer docs">Declare Permissions
</a>
724 <a href=
"#update-permissions" class=
"anchor-link-icon" title=
"This feature mentioned in 'Update app permissions'">↑</a>
727 <a href=
"/apps/storage" title=
"Read 'chrome.storage' in the Chrome developer docs">chrome.storage
</a>
728 <a href=
"#get-and-set" class=
"anchor-link-icon" title=
"This feature mentioned in 'Learn about local.storage.set() and local.storage.get()'">↑</a>
731 <a href=
"/apps/storage#method-StorageArea-get" title=
"Read 'chrome.storage.local.get()' in the Chrome developer docs">chrome.storage.local.get()
</a>
732 <a href=
"#retrieve-items" class=
"anchor-link-icon" title=
"This feature mentioned in 'Retrieve todos items'">↑</a>
735 <a href=
"/apps/storage#method-StorageArea-set" title=
"Read 'chrome.storage.local.set()' in the Chrome developer docs">chrome.storage.local.set()
</a>
736 <a href=
"#save-items" class=
"anchor-link-icon" title=
"This feature mentioned in 'Save todos items'">↑</a>
739 <a href=
"/apps/storage#method-StorageArea-remove" title=
"Read 'chrome.storage.local.remove()' in the Chrome developer docs">chrome.storage.local.remove()
</a>
740 <a href=
"#remove-items" class=
"anchor-link-icon" title=
"This feature mentioned in 'Remove todos items'">↑</a>
743 <a href=
"/apps/storage#method-StorageArea-remove" title=
"Read 'chrome.storage.local.clear()' in the Chrome developer docs">chrome.storage.local.clear()
</a>
744 <a href=
"#remove-items" class=
"anchor-link-icon" title=
"This feature mentioned in 'Drop all todo items'">↑</a>
748 <p>Ready to continue onto the next step? Go to
<a href=
"app_codelab_alarms.html">Step
3 - Add alarms and notifications
»</a></p>