MultiObject Class
MultiObject exposes exactly the same API as each individual object, both functions and events, and the state of all the objects is kept in sync. The objects must maintain all state via Y.Attribute.
MultiObject is similar to Y.ArrayList, except:
- All objects must be of the same type, since MultiObject is supposed to behave exactly like any single object.
- MultiObject automatically delegates all methods.
- By default, MultiObject returns the result from the first object in the list, not an array of results.
- MultiObject propagates all events.
Internally, MultiObject delegates all methods by name, so it supports Y.Do.before, Y.Do.after, etc.
To avoid shadowing potential function names, we inherit from Y.EventTarget and use multi_ as the prefix for our own functions.
Constructor
MultiObject
-
items
-
config
Parameters:
-
items
Arrayinitial set of items
-
config
Objectconfiguration
-
return_all_results=false
BooleanIf this is true, then all delegated functions will return an array of results instead of the result from the primary item. Note that functions which return
undefined
or the item itself always cause the MultiObject to be returned, to support chaining. -
primary_item_index=0
NumberWhen
return_all_results=false
, this is the index of the item which generates the return result for all delegated functions.
-
Item Index
Methods
- _getFullType
- _getType
- _hasPotentialSubscribers
- _monitor
- _parseType
- _publish
- addTarget
- after
- before
- bubble
- detach
- detachAll
- fire
- getEvent
- getTargets
- multi_destroy
- multi_get_all
- multi_get_primary_item_index
- multi_get_return_all_results
- multi_set_primary_item_index
- multi_set_return_all_results
- on
- once
- onceAfter
- parseType
- publish
- removeTarget
- subscribe deprecated
- unsubscribe deprecated
- unsubscribeAll deprecated
Methods
_getFullType
-
type
Parameters:
-
type
StringThe short type to prefix
Returns:
_getType
()
private
_hasPotentialSubscribers
-
fullType
Parameters:
-
fullType
StringThe fully prefixed type name
Returns:
_monitor
-
what
-
eventType
-
o
Parameters:
-
what
String'attach', 'detach', 'fire', or 'publish' -
eventType
String | CustomEventThe prefixed name of the event being monitored, or the CustomEvent object. -
o
ObjectInformation about the event interaction, such as fire() args, subscription category, publish config
_parseType
()
private
_publish
-
fullType
-
etOpts
-
ceOpts
type
to fullType
conversion. It's designed to be a fast
path publish, which can be used by critical code paths to improve performance.
Parameters:
-
fullType
StringThe prefixed type of the event to publish. -
etOpts
ObjectThe EventTarget specific configuration to mix into the published event. -
ceOpts
ObjectThe publish specific configuration to mix into the published event.
Returns:
etOpts
or ceOpts
, this will
be the default CustomEvent
instance, and can be configured independently.
addTarget
-
o
Parameters:
-
o
EventTargetthe target to add
after
-
type
-
fn
-
[context]
-
[arg*]
Parameters:
-
type
StringThe name of the event -
fn
FunctionThe callback to execute in response to the event -
[context]
Object optionalOverridethis
object in callback -
[arg*]
Any optional0..n additional arguments to supply to the subscriber
Returns:
before
()
Returns:
bubble
-
evt
Parameters:
-
evt
CustomEventthe custom event to propagate
Returns:
detach
-
type
-
fn
-
context
Parameters:
-
type
String | ObjectEither the handle to the subscriber or the type of event. If the type is not specified, it will attempt to remove the listener from all hosted events. -
fn
FunctionThe subscribed function to unsubscribe, if not supplied, all subscribers will be removed. -
context
ObjectThe custom object passed to subscribe. This is optional, but if supplied will be used to disambiguate multiple listeners that are the same (e.g., you subscribe many object using a function that lives on the prototype)
Returns:
detachAll
-
type
Parameters:
-
type
StringThe type, or name of the event
fire
-
type
-
arguments
Parameters:
-
type
String | ObjectThe type of the event, or an object that contains a 'type' property. -
arguments
Objectan arbitrary set of parameters to pass to the handler. If the first of these is an object literal and the event is configured to emit an event facade, the event facade will replace that parameter after the properties the object literal contains are copied to the event facade.
Returns:
getEvent
-
type
-
prefixed
Parameters:
-
type
Stringthe type, or name of the event -
prefixed
Stringif true, the type is prefixed already
Returns:
getTargets
()
Returns:
multi_destroy
()
Destroys the MultiObject, but not the individual objects.
destroy()
is, of course, delegated.
multi_get_all
-
f
-
arg
Return an array of all the individual results from calling the specified function. This is only useful if return_all_results=false.
Parameters:
-
f
Stringname of the function to invoke
-
arg
Mixed multiple0..n arguments to pass to the function
Returns:
results from delegating the named function
multi_get_primary_item_index
()
Returns:
the index of the primary item
multi_get_return_all_results
()
Returns:
true if all results will be returned by delegated functions
multi_set_primary_item_index
-
index
Parameters:
-
index
Intthe index of the primary item
multi_set_return_all_results
-
all
Parameters:
-
all
Booleantrue if delegated functions should return all results
on
-
type
-
fn
-
[context]
-
[arg*]
emitFacade = true
will
receive an EventFacade
as the first argument (typically named "e").
These callbacks can then call e.preventDefault()
to disable the
behavior published to that event's defaultFn
. See the EventFacade
API for all available properties and methods. Subscribers to
non-emitFacade
events will receive the arguments passed to fire()
after the event name.
To subscribe to multiple events at once, pass an object as the first
argument, where the key:value pairs correspond to the eventName:callback.
this.on({
"attrChange" : this._onAttrChange,
"change" : this._onChange
});
You can also pass an array of event names as the first argument to
subscribe to all listed events with the same callback.
this.on([ "change", "attrChange" ], this._onChange);
Returning false
from a callback is supported as an alternative to
calling e.preventDefault(); e.stopPropagation();
. However, it is
recommended to use the event methods whenever possible.
Parameters:
-
type
StringThe name of the event -
fn
FunctionThe callback to execute in response to the event -
[context]
Object optionalOverridethis
object in callback -
[arg*]
Any optional0..n additional arguments to supply to the subscriber
Returns:
once
-
type
-
fn
-
[context]
-
[arg*]
on
except the
listener is immediatelly detached when it is executed.
Parameters:
-
type
StringThe name of the event -
fn
FunctionThe callback to execute in response to the event -
[context]
Object optionalOverridethis
object in callback -
[arg*]
Any optional0..n additional arguments to supply to the subscriber
Returns:
onceAfter
-
type
-
fn
-
[context]
-
[arg*]
after
except the
listener is immediatelly detached when it is executed.
Parameters:
-
type
StringThe name of the event -
fn
FunctionThe callback to execute in response to the event -
[context]
Object optionalOverridethis
object in callback -
[arg*]
Any optional0..n additional arguments to supply to the subscriber
Returns:
parseType
-
type
-
[pre]
Parameters:
-
type
Stringthe type -
[pre]
String optionalThe prefix. Defaults to this._yuievt.config.prefix
Returns:
publish
-
type
-
opts
Parameters:
-
type
Stringthe type, or name of the event -
opts
Objectoptional config params. Valid properties are:-
[broadcast=false]
Boolean optionalwhether or not the YUI instance and YUI global are notified when the event is fired.
-
[bubbles=true]
Boolean optionalWhether or not this event bubbles. Events can only bubble if
emitFacade
is true. -
[context=this]
Object optionalthe default execution context for the listeners.
-
[defaultFn]
Function optionalthe default function to execute when this event fires if preventDefault was not called.
-
[emitFacade=false]
Boolean optionalwhether or not this event emits a facade.
-
[prefix]
String optionalthe prefix for this targets events, e.g., 'menu' in 'menu:click'.
-
[fireOnce=false]
Boolean optionalif an event is configured to fire once, new subscribers after the fire will be notified immediately.
-
[async=false]
Boolean optionalfireOnce event listeners will fire synchronously if the event has already fired unless
async
istrue
. -
[preventable=true]
Boolean optionalwhether or not
preventDefault()
has an effect. -
[preventedFn]
Function optionala function that is executed when
preventDefault()
is called. -
[queuable=false]
Boolean optionalwhether or not this event can be queued during bubbling.
-
[silent]
Boolean optionalif silent is true, debug messages are not provided for this event.
-
[stoppedFn]
Function optionala function that is executed when stopPropagation is called.
-
[monitored]
Boolean optionalspecifies whether or not this event should send notifications about when the event has been attached, detached, or published.
-
[type]
String optionalthe event type (valid option if not provided as the first parameter to publish).
-
Returns:
subscribe
()
deprecated
unsubscribe
()
deprecated
unsubscribeAll
-
type
Parameters:
-
type
StringThe type, or name of the event