View
 

Runtime-Enumerables Arrays and Sets

Page history last edited by Charles Jolley 9 mos ago

JavaScript comes with two very primitive types of collections: hash and array.  All objects in JavaScript are hashes.  All arrays are instances of the built-in Array object.  In a large application, however, these two primitive types are often not enough.  For one thing, they may not be fast enough for certain large data set.  They also won't work with Key Value Observing out of the box.

 

SproutCore defines a new generic interface for working with collections of items called SC.Enumerable.  Unlike the built-in Array object in JavaScript, SC.Enumerable can be applied to any object, meaning that you can construct your own, new types of collections as well as using native Arrays.  In addition, the interface defined in SC.Enumerable (and it's companion mixin SC.Array) are KVO-aware.

 

SC.Enumerable and SC.Array are both added automatically to the built-in Array class in JavaScript.  In addition, SproutCore includes a number of additional pre-defined enumerable objects that have different efficiencies such as SC.Set an SC.IndexSet.

 

The subsections below describe these mixins and built-in objects in more detail.

 

SC.Enumerable

 

The core mixin interface applied to all enumerables including Array.  Use this interface anytime you want to work with a generic ordered or unordered enumerable.

 

Learn more about SC.Enumerable »

 

SC.Array

 

An additional mixin applied to ordered enumerables such as Array.  Use this interface anytime you want to work with an ordered enumerable.  Unlike the built-in Array methods, SC.Array methods are KVO aware and will update your observers properly.

 

Learn more about SC.Array »

 

SC.Set, SC.IndexSet, SC.SelectionSet

 

A set is an unordered collection of objects.  Unlike an Array, a set does not retain a specific order to its content objects.  However, it can add or remove object and determine set membership very quickly.  SproutCore comes with several types of set collections that manage objects, numbers or a selection of items. 

 

Use a Set instead of an Array whenever you might have a large number of objects, you don't care about the order, but you want to add or remove items very quickly.

 

Learn more about SC.Set, SC.IndexSet, SC.SelectionSet »

 

SC.SparseArray

 

A Sparse Array can be used anytime you want to work with a collection of objects as if it was an array without having to build the array until you actually need it.  Sparse array's respond to normal SC.Array methods, but they ask a delegate to provide the data for the array on demand.

 

Learn more about SC.SparseArray »

 

Reducers

 

Reducers are an enhancement on SC.Array that can automatically compute summary values for arrays.  Reducers are an incredibly useful way to keep track of overall summary information on a collection.

 

Learn more about Reducers »

 

 

Moving On 

 

or Back to Runtime Programming Guide Home »

Comments (0)

You don't have permission to comment on this page.