An unordered collection of objects.
A Set works a bit like an array except that its items are not ordered. You can create a set to efficiently test for membership for an object. You can also iterate through a set just like an array, even accessing objects by index, however there is no gaurantee as to their order.
Whether or not property observing is enabled, sets offer very powerful
notifications of items being added and removed, through the
removeSetObserver methods; this can be
very useful, for instance, for filtering or mapping sets.
SC.Set is a primitive object, like an array. It does implement
limited key-value observing support, but it does not extend from
so you should not subclass it.
Creating a Set
You can create a set like you would most objects using
Most new sets you create will be empty, but you can also initialize the set
with some content by passing an array or other enumerable of objects to the
Finally, you can pass in an existing set and the set will be copied. You
can also create a copy of a set by calling
// creates a new empty set var foundNames = SC.Set.create(); // creates a set with four names in it. var names = SC.Set.create(["Charles", "Tom", "Juan", "Alex"]); // creates a copy of the names set. var namesCopy = SC.Set.create(names); // same as above. var anotherNamesCopy = names.clone();
You generally add or remove objects from a set using add() or remove(). You can add any type of object including primitives such as numbers, strings, and booleans.
Note that objects can only exist one time in a set. If you call add() on a set with the same object multiple times, the object will only be added once. Likewise, calling remove() with the same object multiple times will remove the object the first time and have no effect on future calls until you add the object to the set again.
Note that you cannot add/remove null or undefined to a set. Any attempt to do so will be ignored.
In addition to add/remove you can also call push()/pop(). Push behaves just like add() but pop(), unlike remove() will pick an arbitrary object, remove it and return it. This is a good way to use a set as a job queue when you don't care which order the jobs are executed in.
Testing for an Object
To test for an object's presence in a set you simply call
This method tests for the object's hash, which is generally the same as the
object's guid; however, if you implement the hash() method on the object, it will
use the return value from that method instead.
SC.Set (rather than
SC.CoreSet), you can observe the
"" property to be alerted whenever the content changes.
This is often unhelpful. If you are filtering sets of objects, for instance, it is very inefficient to re-filter all of the items each time the set changes. It would be better if you could just adjust the filtered set based on what was changed on the original set. The same issue applies to merging sets, as well.
SC.CoreSet both offer another method of being observed:
removeSetObserver. These take a single parameter:
an object which implements
didAddItem(set, item) and
Whenever an item is added or removed from the set, all objects in the set
SC.CoreSet, actually) of observing objects will be alerted appropriately.
SetObservers are not intended to be used "_creatively_"; for instance, do not expect to be alerted immediately to any changes. *While the notifications are currently sent out immediately, if we find a fast way to send them at end of run loop, we most likely will do so.*
Defined in: set.js
- SproutCore 1.0
Field DetailisSet Boolean
Instance Method Detail
Call this method to add an object. performs a basic add.
If the object is already in the set it will not be added again.
Adds a set observers. Set observers must implement two methods:
Set observers are, in fact, stored in another set (a CoreSet).
- new copy
Creates a new set, with the optional array of items included in the return set.
YES if the passed object is also a set that contains the same
objects as the receiver.
- an object from the set or null
Removes the object from the set if it is found.
If the object is not in the set, nothing will be changed.