Print Friendly

# Class Ext.util.MixedCollection

Package: | Ext.util
Class: | MixedCollection
Extends: | Observable
Defined In: | MixedCollection.js

A Collection class that maintains both numeric indexes and keys and exposes events.
Properties - Methods - Events

## Public Properties

This class has no public properties.

## Public Methods

Method | Defined By
--- | ---
| MixedCollection(`Boolean allowFunctions`, `Function keyFn`) | MixedCollection
| add(`String key`, `Object o`) : Object | MixedCollection
Adds an item to the collection.
| addAll(`Object/Array objs`) : void | MixedCollection
Adds all elements of an Array or an Object to the collection.
| addEvents(`Object object`) : void | Observable
Copies any events from the passed object onto this object if they do not already exist. The passed object must also ...
| addListener(`String eventName`, `Function handler`, [`Object scope`], [`Object options`]) : void | Observable
Appends an event handler to this component
| clear() : void | MixedCollection
Removes all items from the collection.
| clone() : MixedCollection | MixedCollection
Creates a duplicate of this collection
| contains(`Object o`) : Boolean | MixedCollection
Returns true if the collection contains the passed Object as an item.
| containsKey(`String key`) : Boolean | MixedCollection
Returns true if the collection contains the passed Object as a key.
| each(`Function fn`, [`Object scope`]) : void | MixedCollection
Executes the specified function once for every item in the collection, passing each item as the first and only parame...
| eachKey(`Function fn`, [`Object scope`]) : void | MixedCollection
Executes the specified function once for every key in the collection, passing each key, and its associated item as th...
| filter(`String property`, `String/RegExp value`) : MixedCollection | MixedCollection
Filter the objects in this collection by a specific property. Returns a new collection that has been filtered.
| filterBy(`Function fn`, [`Object scope`]) : MixedCollection | MixedCollection
Filter by a function. * Returns a new collection that has been filtered. The passed function will be called with each...
| find(`Function fn`, [`Object scope`]) : Object | MixedCollection
Returns the first item in the collection which elicits a true return value from the passed selection function.
| fireEvent(`String eventName`, `Object... args`) : Boolean | Observable
Fires the specified event with the passed parameters (minus the event name).
| first() : Object | MixedCollection
Returns the first item in the collection.
| get(`String/Number key`) : Object | MixedCollection
Returns the item associated with the passed key or index.
| getCount() : Number | MixedCollection
Returns the number of items in the collection.
| getKey(`o {Object}`) : Object | MixedCollection
MixedCollection has a generic way to fetch keys if you implement getKey. // normal way var mc = new E...
| getRange([`Number startIndex`], [`Number endIndex`]) : Array | MixedCollection
Returns a range of items in this collection
| hasListener(`String eventName`) : Boolean | Observable
Checks to see if this object is currently listening for a specified event
| indexOf(`Object o`) : Number | MixedCollection
Returns index within the collection of the passed Object.
| indexOfKey(`String key`) : Number | MixedCollection
Returns index within the collection of the passed key.
| insert(`Number index`, `String key`, [`Object o`]) : Object | MixedCollection
Inserts an item at the specified index in the collection.
| item(`String/Number key`) : Object | MixedCollection
Returns the item associated with the passed key OR index. Key has priority over index.
| itemAt(`Number index`) : Object | MixedCollection
Returns the item at the specified index.
| key(`String/Number key`) : Object | MixedCollection
Returns the item associated with the passed key.
| keySort([`String direction`], [`Function fn`]) : void | MixedCollection
Sorts this collection by keys
| last() : Object | MixedCollection
Returns the last item in the collection.
| on(`String eventName`, `Function handler`, [`Object options`]) : void | Observable
Appends an event handler to this element (shorthand for addListener)
| purgeListeners() : void | Observable
Removes all listeners for this object
| remove(`Object o`) : Object | MixedCollection
Removed an item from the collection.
| removeAt(`Number index`) : void | MixedCollection
Remove an item from a specified index in the collection.
| removeKey(`String key`) : void | MixedCollection
Removed an item associated with the passed key fom the collection.
| removeListener(`String eventName`, `Function handler`, [`Object scope`]) : void | Observable
Removes a listener
| replace(`String key`, [`o {Object}`]) : Object | MixedCollection
Replaces an item in the collection.
| sort([`String direction`], [`Function fn`]) : void | MixedCollection
Sorts this collection with the passed comparison function
| un(`String eventName`, `Function handler`, [`Object scope`]) : void | Observable
Removes a listener (shorthand for removeListener)

## Public Events

Event | Defined By
--- | ---
| add : (`Number index`, `Object o`, `String key`) | MixedCollection
Fires when an item is added to the collection.
| clear : () | MixedCollection
Fires when the collection is cleared.
| remove : (`Object o`, [`String key`]) | MixedCollection
Fires when an item is removed from the collection.
| replace : (`String key`, `Object old`, `Object new`) | MixedCollection
Fires when an item is replaced in the collection.

## Constructor Details

### MixedCollection

`public function MixedCollection(`Boolean allowFunctions`, `Function keyFn`)`
Parameters:

- `allowFunctions` : Boolean
  True if the addAll function should add function references to the collection (defaults to false)
- `keyFn` : Function
  A function that can accept an item of the type(s) stored in this MixedCollection and return the key value for that item. This is used when available to look up the key on items that were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is equivalent to providing an implementation for the getKey method.

## Method Details

### add

`public function add(`String key`, `Object o`)`
Adds an item to the collection.
Parameters:

- `key` : String
  The key to associate with the item
- `o` : Object
  The item to add.

Returns:

- `Object`
  The item added.

This method is defined by MixedCollection.

### addAll

`public function addAll(`Object/Array objs`)`
Adds all elements of an Array or an Object to the collection.
Parameters:

- `objs` : Object/Array
  An Object containing properties which will be added to the collection, or an Array of values, each of which are added to the collection.

Returns:

- `void`

This method is defined by MixedCollection.

### addEvents

`public function addEvents(`Object object`)`
Copies any events from the passed object onto this object if they do not already exist. The passed object must also inherit from Observable for this method to have any effect.
Parameters:

- `object` : Object
  The object from which to copy events

Returns:

- `void`

This method is defined by Observable.

### addListener

`public function addListener(`String eventName`, `Function handler`, [`Object scope`], [`Object options`])`
Appends an event handler to this component
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The method the event invokes
- `scope` : Object
  (optional) The scope in which to execute the handler function. The handler function's "this" context.
- `options` : Object
  (optional) An object containing handler configuration properties. This may contain any of the following properties:
  - scope {Object} The scope in which to execute the handler function. The handler function's "this" context.
  - delegate {String} A simple selector to filter the target or look for a descendant of the target
  - stopEvent {Boolean} True to stop the event. That is stop propagation, and prevent the default action.
  - preventDefault {Boolean} True to prevent the default action
  - stopPropagation {Boolean} True to prevent event propagation
  - normalized {Boolean} False to pass a browser event to the handler function instead of an Ext.EventObject
  - delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.
  - single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.
  - buffer {Number} Causes the handler to be scheduled to run in an Ext.util.DelayedTask delayed by the specified number of milliseconds. If the event fires again within that time, the original handler is not invoked, but the new handler is scheduled in its place.

  Combining Options
  Using the options argument, it is possible to combine different types of listeners:
  A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)

  Code:

      el.on('click', this.onClick, this, { single: true, delay: 100, stopEvent : true, forumId: 4 });

  The method also allows for a single argument to be passed which is a config object containing properties which specify multiple handlers.

  Attaching multiple handlers in 1 call
  Code:

      el.on({ 'click' : { fn: this.onClick scope: this, delay: 100 }, 'mouseover' : { fn: this.onMouseOver scope: this }, 'mouseout' : { fn: this.onMouseOut scope: this } });

  Or a shorthand syntax:
  Code:

      el.on({ 'click' : this.onClick, 'mouseover' : this.onMouseOver, 'mouseout' : this.onMouseOut scope: this });

Returns:

- `void`

This method is defined by Observable.

### clear

`public function clear()`
Removes all items from the collection.
Parameters:

- None.

Returns:

- `void`

This method is defined by MixedCollection.

### clone

`public function clone()`
Creates a duplicate of this collection
Parameters:

- None.

Returns:

- `MixedCollection`

This method is defined by MixedCollection.

### contains

`public function contains(`Object o`)`
Returns true if the collection contains the passed Object as an item.
Parameters:

- `o` : Object
  The Object to look for in the collection.

Returns:

- `Boolean`
  True if the collection contains the Object as an item.

This method is defined by MixedCollection.

### containsKey

`public function containsKey(`String key`)`
Returns true if the collection contains the passed Object as a key.
Parameters:

- `key` : String
  The key to look for in the collection.

Returns:

- `Boolean`
  True if the collection contains the Object as a key.

This method is defined by MixedCollection.

### each

`public function each(`Function fn`, [`Object scope`])`
Executes the specified function once for every item in the collection, passing each item as the first and only parameter. returning false from the function will stop the iteration.
Parameters:

- `fn` : Function
  The function to execute for each item.
- `scope` : Object
  (optional) The scope in which to execute the function.

Returns:

- `void`

This method is defined by MixedCollection.

### eachKey

`public function eachKey(`Function fn`, [`Object scope`])`
Executes the specified function once for every key in the collection, passing each key, and its associated item as the first two parameters.
Parameters:

- `fn` : Function
  The function to execute for each item.
- `scope` : Object
  (optional) The scope in which to execute the function.

Returns:

- `void`

This method is defined by MixedCollection.

### filter

`public function filter(`String property`, `String/RegExp value`)`
Filter the objects in this collection by a specific property. Returns a new collection that has been filtered.
Parameters:

- `property` : String
  A property on your objects
- `value` : String/RegExp
  Either string that the property values should start with or a RegExp to test against the property

Returns:

- `MixedCollection`
  The new filtered collection

This method is defined by MixedCollection.

### filterBy

`public function filterBy(`Function fn`, [`Object scope`])`
Filter by a function. * Returns a new collection that has been filtered. The passed function will be called with each object in the collection. If the function returns true, the value is included otherwise it is filtered.
Parameters:

- `fn` : Function
  The function to be called, it will receive the args o (the object), k (the key)
- `scope` : Object
  (optional) The scope of the function (defaults to this)

Returns:

- `MixedCollection`
  The new filtered collection

This method is defined by MixedCollection.

### find

`public function find(`Function fn`, [`Object scope`])`
Returns the first item in the collection which elicits a true return value from the passed selection function.
Parameters:

- `fn` : Function
  The selection function to execute for each item.
- `scope` : Object
  (optional) The scope in which to execute the function.

Returns:

- `Object`
  The first item in the collection which returned true from the selection function.

This method is defined by MixedCollection.

### fireEvent

`public function fireEvent(`String eventName`, `Object... args`)`
Fires the specified event with the passed parameters (minus the event name).
Parameters:

- `eventName` : String
- `args` : Object...
  Variable number of parameters are passed to handlers

Returns:

- `Boolean`
  returns false if any of the handlers return false otherwise it returns true

This method is defined by Observable.

### first

`public function first()`
Returns the first item in the collection.
Parameters:

- None.

Returns:

- `Object`
  the first item in the collection..

This method is defined by MixedCollection.

### get

`public function get(`String/Number key`)`
Returns the item associated with the passed key or index.
Parameters:

- `key` : String/Number
  The key or index of the item.

Returns:

- `Object`
  The item associated with the passed key.

This method is defined by MixedCollection.

### getCount

`public function getCount()`
Returns the number of items in the collection.
Parameters:

- None.

Returns:

- `Number`
  the number of items in the collection.

This method is defined by MixedCollection.

### getKey

`public function getKey(`o {Object}`)`
MixedCollection has a generic way to fetch keys if you implement getKey.

        // normal way
        var mc = new Ext.util.MixedCollection();
        mc.add(someEl.dom.id, someEl);
        mc.add(otherEl.dom.id, otherEl);
        //and so on

        // using getKey
        var mc = new Ext.util.MixedCollection();
        mc.getKey = function(el){
           return el.dom.id;
        }
        mc.add(someEl);
        mc.add(otherEl);
        // etc

                        Parameters:
                        {Object} : oThe item for which to find the key.
                        Returns:

                            ObjectThe key for the passed item.

This method is defined by MixedCollection.

### getRange

`public function getRange([`Number startIndex`], [`Number endIndex`])`
Returns a range of items in this collection
Parameters:

- `startIndex` : Number
  (optional) defaults to 0
- `endIndex` : Number
  (optional) default to the last item

Returns:

- `Array`
  An array of items

This method is defined by MixedCollection.

### hasListener

`public function hasListener(`String eventName`)`
Checks to see if this object is currently listening for a specified event
Parameters:

- `eventName` : String
  The name of the event to check for

Returns:

- `Boolean`
  True if the event is being listened for, else false

This method is defined by Observable.

### indexOf

`public function indexOf(`Object o`)`
Returns index within the collection of the passed Object.
Parameters:

- `o` : Object
  The item to find the index of.

Returns:

- `Number`
  index of the item.

This method is defined by MixedCollection.

### indexOfKey

`public function indexOfKey(`String key`)`
Returns index within the collection of the passed key.
Parameters:

- `key` : String
  The key to find the index of.

Returns:

- `Number`
  index of the key.

This method is defined by MixedCollection.

### insert

`public function insert(`Number index`, `String key`, [`Object o`])`
Inserts an item at the specified index in the collection.
Parameters:

- `index` : Number
  The index to insert the item at.
- `key` : String
  The key to associate with the new item, or the item itself.
- `o` : Object
  (optional) If the second parameter was a key, the new item.

Returns:

- `Object`
  The item inserted.

This method is defined by MixedCollection.

### item

`public function item(`String/Number key`)`
Returns the item associated with the passed key OR index. Key has priority over index.
Parameters:

- `key` : String/Number
  The key or index of the item.

Returns:

- `Object`
  The item associated with the passed key.

This method is defined by MixedCollection.

### itemAt

`public function itemAt(`Number index`)`
Returns the item at the specified index.
Parameters:

- `index` : Number
  The index of the item.

Returns:

- `Object`

This method is defined by MixedCollection.

### key

`public function key(`String/Number key`)`
Returns the item associated with the passed key.
Parameters:

- `key` : String/Number
  The key of the item.

Returns:

- `Object`
  The item associated with the passed key.

This method is defined by MixedCollection.

### keySort

`public function keySort([`String direction`], [`Function fn`])`
Sorts this collection by keys
Parameters:

- `direction` : String
  (optional) "ASC" or "DESC"
- `fn` : Function
  (optional) a comparison function (defaults to case insensitive string)

Returns:

- `void`

This method is defined by MixedCollection.

### last

`public function last()`
Returns the last item in the collection.
Parameters:

- None.

Returns:

- `Object`
  the last item in the collection..

This method is defined by MixedCollection.

### on

`public function on(`String eventName`, `Function handler`, [`Object options`])`
Appends an event handler to this element (shorthand for addListener)
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The method the event invokes
- `options` : Object
  (optional)

Returns:

- `void`

This method is defined by Observable.

### purgeListeners

`public function purgeListeners()`
Removes all listeners for this object
Parameters:

- None.

Returns:

- `void`

This method is defined by Observable.

### remove

`public function remove(`Object o`)`
Removed an item from the collection.
Parameters:

- `o` : Object
  The item to remove.

Returns:

- `Object`
  The item removed.

This method is defined by MixedCollection.

### removeAt

`public function removeAt(`Number index`)`
Remove an item from a specified index in the collection.
Parameters:

- `index` : Number
  The index within the collection of the item to remove.

Returns:

- `void`

This method is defined by MixedCollection.

### removeKey

`public function removeKey(`String key`)`
Removed an item associated with the passed key fom the collection.
Parameters:

- `key` : String
  The key of the item to remove.

Returns:

- `void`

This method is defined by MixedCollection.

### removeListener

`public function removeListener(`String eventName`, `Function handler`, [`Object scope`])`
Removes a listener
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The handler to remove
- `scope` : Object
  (optional) The scope (this object) for the handler

Returns:

- `void`

This method is defined by Observable.

### replace

`public function replace(`String key`, [`o {Object}`])`
Replaces an item in the collection.
Parameters:

- `key` : String
  The key associated with the item to replace, or the item to replace.
- `{Object}` : o
  o (optional) If the first parameter passed was a key, the item to associate with that key.

Returns:

- `Object`
  The new item.

This method is defined by MixedCollection.

### sort

`public function sort([`String direction`], [`Function fn`])`
Sorts this collection with the passed comparison function
Parameters:

- `direction` : String
  (optional) "ASC" or "DESC"
- `fn` : Function
  (optional) comparison function

Returns:

- `void`

This method is defined by MixedCollection.

### un

`public function un(`String eventName`, `Function handler`, [`Object scope`])`
Removes a listener (shorthand for removeListener)
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The handler to remove
- `scope` : Object
  (optional) The scope (this object) for the handler

Returns:

- `void`

This method is defined by Observable.

## Event Details

### add

`public event add`
Fires when an item is added to the collection.
Subscribers will be called with the following parameters:

- `index` : Number
  The index at which the item was added.
- `o` : Object
  The item added.
- `key` : String
  The key associated with the added item.

This event is defined by MixedCollection.

### clear

`public event clear`
Fires when the collection is cleared.
Subscribers will be called with the following parameters:

- None.

This event is defined by MixedCollection.

### remove

`public event remove`
Fires when an item is removed from the collection.
Subscribers will be called with the following parameters:

- `o` : Object
  The item being removed.
- `key` : String
  (optional) The key associated with the removed item.

This event is defined by MixedCollection.

### replace

`public event replace`
Fires when an item is replaced in the collection.
Subscribers will be called with the following parameters:

- `key` : String
  he key associated with the new added.
- `old` : Object
  The item being replaced.
- `new` : Object
  The new item.

This event is defined by MixedCollection.

Ext - Copyright © 2006-2007 Ext JS, LLC
All rights reserved.