1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.HashMap-method-constructor'><span id='Ext-util.HashMap'>/**
2 </span></span> * @class Ext.util.HashMap
4 * Represents a collection of a set of key and value pairs. Each key in the HashMap
5 * must be unique, the same key cannot exist twice. Access to items is provided via
6 * the key only. Sample usage:
7 * <pre><code>
8 var map = new Ext.util.HashMap();
13 map.each(function(key, value, length){
14 console.log(key, value, length);
16 * </code></pre>
19 * <p>The HashMap is an unordered class,
20 * there is no guarantee when iterating over the items that they will be in any particular
21 * order. If this is required, then use a {@link Ext.util.MixedCollection}.
24 * @param {Object} config The configuration options
26 Ext.define('Ext.util.HashMap', {
28 <span id='Ext-util.HashMap-cfg-keyFn'> /**
29 </span> * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
30 * A default is provided that returns the <b>id</b> property on the object. This function is only used
31 * if the add method is called with a single argument.
35 observable: 'Ext.util.Observable'
38 constructor: function(config) {
42 <span id='Ext-util.HashMap-event-add'> /**
44 * Fires when a new item is added to the hash
45 * @param {Ext.util.HashMap} this.
46 * @param {String} key The key of the added item.
47 * @param {Object} value The value of the added item.
50 <span id='Ext-util.HashMap-event-clear'> /**
51 </span> * @event clear
52 * Fires when the hash is cleared.
53 * @param {Ext.util.HashMap} this.
56 <span id='Ext-util.HashMap-event-remove'> /**
57 </span> * @event remove
58 * Fires when an item is removed from the hash.
59 * @param {Ext.util.HashMap} this.
60 * @param {String} key The key of the removed item.
61 * @param {Object} value The value of the removed item.
64 <span id='Ext-util.HashMap-event-replace'> /**
65 </span> * @event replace
66 * Fires when an item is replaced in the hash.
67 * @param {Ext.util.HashMap} this.
68 * @param {String} key The key of the replaced item.
69 * @param {Object} value The new value for the item.
70 * @param {Object} old The old value for the item.
75 me.mixins.observable.constructor.call(me, config);
79 <span id='Ext-util.HashMap-method-getCount'> /**
80 </span> * Gets the number of items in the hash.
81 * @return {Number} The number of items in the hash.
83 getCount: function() {
87 <span id='Ext-util.HashMap-method-getData'> /**
88 </span> * Implementation for being able to extract the key from an object if only
89 * a single argument is passed.
91 * @param {String} key The key
92 * @param {Object} value The value
93 * @return {Array} [key, value]
95 getData: function(key, value) {
96 // if we have no value, it means we need to get the key from the object
97 if (value === undefined) {
99 key = this.getKey(value);
105 <span id='Ext-util.HashMap-method-getKey'> /**
106 </span> * Extracts the key from an object. This is a default implementation, it may be overridden
108 * @param {Object} o The object to get the key from
109 * @return {String} The key to use.
111 getKey: function(o) {
115 <span id='Ext-util.HashMap-method-add'> /**
116 </span> * Adds an item to the collection. Fires the {@link #add} event when complete.
117 * @param {String} key <p>The key to associate with the item, or the new item.</p>
118 * <p>If a {@link #getKey} implementation was specified for this HashMap,
119 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
120 * the HashMap will be able to <i>derive</i> the key for the new item.
121 * In this case just pass the new item in this parameter.</p>
122 * @param {Object} o The item to add.
123 * @return {Object} The item added.
125 add: function(key, value) {
129 if (arguments.length === 1) {
131 key = me.getKey(value);
134 if (me.containsKey(key)) {
135 me.replace(key, value);
138 data = me.getData(key, value);
143 me.fireEvent('add', me, key, value);
147 <span id='Ext-util.HashMap-method-replace'> /**
148 </span> * Replaces an item in the hash. If the key doesn't exist, the
149 * {@link #add} method will be used.
150 * @param {String} key The key of the item.
151 * @param {Object} value The new value for the item.
152 * @return {Object} The new value of the item.
154 replace: function(key, value) {
159 if (!me.containsKey(key)) {
164 me.fireEvent('replace', me, key, value, old);
168 <span id='Ext-util.HashMap-method-remove'> /**
169 </span> * Remove an item from the hash.
170 * @param {Object} o The value of the item to remove.
171 * @return {Boolean} True if the item was successfully removed.
173 remove: function(o) {
174 var key = this.findKey(o);
175 if (key !== undefined) {
176 return this.removeAtKey(key);
181 <span id='Ext-util.HashMap-method-removeAtKey'> /**
182 </span> * Remove an item from the hash.
183 * @param {String} key The key to remove.
184 * @return {Boolean} True if the item was successfully removed.
186 removeAtKey: function(key) {
190 if (me.containsKey(key)) {
194 me.fireEvent('remove', me, key, value);
200 <span id='Ext-util.HashMap-method-get'> /**
201 </span> * Retrieves an item with a particular key.
202 * @param {String} key The key to lookup.
203 * @return {Object} The value at that key. If it doesn't exist, <tt>undefined</tt> is returned.
206 return this.map[key];
209 <span id='Ext-util.HashMap-method-clear'> /**
210 </span> * Removes all items from the hash.
211 * @return {Ext.util.HashMap} this
213 clear: function(/* private */ initial) {
217 if (initial !== true) {
218 me.fireEvent('clear', me);
223 <span id='Ext-util.HashMap-method-containsKey'> /**
224 </span> * Checks whether a key exists in the hash.
225 * @param {String} key The key to check for.
226 * @return {Boolean} True if they key exists in the hash.
228 containsKey: function(key) {
229 return this.map[key] !== undefined;
232 <span id='Ext-util.HashMap-method-contains'> /**
233 </span> * Checks whether a value exists in the hash.
234 * @param {Object} value The value to check for.
235 * @return {Boolean} True if the value exists in the dictionary.
237 contains: function(value) {
238 return this.containsKey(this.findKey(value));
241 <span id='Ext-util.HashMap-method-getKeys'> /**
242 </span> * Return all of the keys in the hash.
243 * @return {Array} An array of keys.
245 getKeys: function() {
246 return this.getArray(true);
249 <span id='Ext-util.HashMap-method-getValues'> /**
250 </span> * Return all of the values in the hash.
251 * @return {Array} An array of values.
253 getValues: function() {
254 return this.getArray(false);
257 <span id='Ext-util.HashMap-method-getArray'> /**
258 </span> * Gets either the keys/values in an array from the hash.
260 * @param {Boolean} isKey True to extract the keys, otherwise, the value
261 * @return {Array} An array of either keys/values from the hash.
263 getArray: function(isKey) {
268 if (map.hasOwnProperty(key)) {
269 arr.push(isKey ? key: map[key]);
275 <span id='Ext-util.HashMap-method-each'> /**
276 </span> * Executes the specified function once for each item in the hash.
277 * Returning false from the function will cease iteration.
279 * The paramaters passed to the function are:
280 * <div class="mdetail-params"><ul>
281 * <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
282 * <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
283 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the hash</p></li>
284 * </ul></div>
285 * @param {Function} fn The function to execute.
286 * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
287 * @return {Ext.util.HashMap} this
289 each: function(fn, scope) {
290 // copy items so they may be removed during iteration.
291 var items = Ext.apply({}, this.map),
293 length = this.length;
295 scope = scope || this;
297 if (items.hasOwnProperty(key)) {
298 if (fn.call(scope, key, items[key], length) === false) {
306 <span id='Ext-util.HashMap-method-clone'> /**
307 </span> * Performs a shallow copy on this hash.
308 * @return {Ext.util.HashMap} The new hash object.
311 var hash = new this.self(),
315 hash.suspendEvents();
317 if (map.hasOwnProperty(key)) {
318 hash.add(key, map[key]);
325 <span id='Ext-util.HashMap-method-findKey'> /**
327 * Find the key for a value.
328 * @param {Object} value The value to find.
329 * @return {Object} The value of the item. Returns <tt>undefined</tt> if not found.
331 findKey: function(value) {
336 if (map.hasOwnProperty(key) && map[key] === value) {
343 </pre></pre></body></html>