delegate
can/map/delegateobserve.delegate( selector, event, handler )
Parameters
-
selector
{String}
The attributes you want to listen for changes in.
Selector should be the property or property names of the element you are searching. Examples:
- "name" - listens to the "name" property changing
- "name, address" - listens to "name" or "address" changing
- "name address" - listens to "name" or "address" changing
- "address.*" - listens to property directly in address
- "address.**" - listens to any property change in address
- "foo=bar" - listens when foo is "bar"
-
event
{String}
The event name. One of ("set","add","remove","change")
-
handler
{function(ev, newVal, oldVal, prop)}
The callback handler called with:
- newVal - the new value set on the observe
- oldVal - the old value set on the observe
- prop - the prop name that was changed
Returns
{can.Map}
the observe for chaining
delegate( selector, event, handler(ev,newVal,oldVal,from) )
listen for changes in a child attribute from the parent. The child attribute does not have to exist.Types of events
Delegate lets you listen to add, set, remove, and change events on property.
add
An add event is fired when a new property has been added.
Listening to add events is useful for 'setup' functionality (in this case showing the
#name
element.set
Set events are fired when a property takes on a new value. set events are always fired after an add.
remove
Remove events are fired after a property is removed.
Wildcards - matching multiple properties
Sometimes, you want to know when any property within some part of an observe has changed. Delegate lets you use wildcards to match any property name. The following listens for any change on an attribute of the params attribute:
Using a single wildcard (
*
) matches single level properties. Using a double wildcard (**
) matches any deep property.Listening on multiple properties and values
Delegate lets you listen on multiple values at once. The following listens for first and last name changes:
Listening when properties are a particular value
Delegate lets you listen when a property is set to a specific value: