Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
|
Evan Stade
2011/07/11 21:31:52
namespace the stuff in this file via cr.define
Scott Franklin
2011/07/12 22:35:13
Somehow I missed that everything else in here was
| |
| 5 // This class stores hashes by their id field and provides basic methods for | |
| 6 // iterating over the collection. | |
| 7 function ItemStore() { | |
| 8 this.items_ = {}; | |
| 9 }; | |
| 10 | |
| 11 // Gets a sorted list of ids. | |
| 12 ItemStore.prototype.ids = function() { | |
|
Evan Stade
2011/07/11 21:31:52
ItemStore.prototype = {
ids: function() {
},
Scott Franklin
2011/07/12 22:35:13
Oh, handy. Done.
| |
| 13 var ids = []; | |
| 14 for (var i in this.items_) | |
| 15 ids.push(i); | |
| 16 return ids.sort(); | |
| 17 }; | |
| 18 | |
| 19 // Adds an item to the store. | |
| 20 ItemStore.prototype.addItem = function(item) { | |
| 21 this.items_[item.id] = item; | |
| 22 }; | |
| 23 | |
| 24 // Adds a hash of items to the store. | |
| 25 ItemStore.prototype.addItems = function(items) { | |
| 26 for (id in items) | |
| 27 this.addItem(items[id]); | |
| 28 }; | |
| 29 | |
| 30 // Removes an item from the store. | |
| 31 ItemStore.prototype.removeItem = function(id) { | |
| 32 delete this.items_[id]; | |
| 33 }; | |
| 34 | |
| 35 // Maps the itemStore to an Array. Items are sorted by id. | |
| 36 ItemStore.prototype.map = function(mapper) { | |
|
Evan Stade
2011/07/11 21:31:52
jsdoc style documentation for functions and their
Scott Franklin
2011/07/12 22:35:13
Done.
| |
| 37 var items = this.items_; | |
| 38 var ids = this.ids(); | |
| 39 return ids.map(function(id) { return mapper(items[id]); }); | |
| 40 }; | |
| OLD | NEW |