attr
can.Map.prototype.attr
Get or set properties on a Map.
map.attr()
Gets a collection of all the properties in this can.Map
.
Returns
{Object}
an object with all the properties in this can.Map
.
map.attr(key)
Reads a property from this can.Map
.
Parameters
-
key
{String}
the property to read
Returns
{*}
the value assigned to key.
map.attr(key, value)
Assigns value to a property on this can.Map
called key.
Parameters
-
key
{String}
the property to set
-
value
{*}
the value to assign to key.
Returns
{can.Map}
this Map, for chaining
map.attr(obj[, removeOthers])
Assigns each value in obj to a property on this can.Map
named after the
corresponding key in obj, effectively merging obj into the Map.
Parameters
-
obj
{Object}
a collection of key-value pairs to set. If any properties already exist on the
can.Map
, they will be overwritten. -
removeOthers
=false
{bool}
Optionalwhether to remove keys not present in obj. To remove keys without setting other keys, use
removeAttr
.
Returns
{can.Map}
this Map, for chaining
attr
gets or sets properties on thecan.Map
it's called on. Here's a tour through how all of its forms work:Deep properties
attr
can also set and read deep properties. All you have to do is specify the property name as you normally would if you weren't usingattr
.Objects that are added to Observes become Observes themselves behind the scenes, so changes to deep properties fire events at each level, and you can bind at any level. As this example shows, all the same events are fired no matter what level you call
attr
at:Properties with dots in their name
As shown above,
attr
enables reading and setting deep properties so special care must be taken when property names include dots '.
'. To read a property containing dots, escape each one using '\
'. This preventsattr
from performing a deep lookup and throwing an error when the deep property is not found.When setting a property containing dots, pass an object to
attr
containing the property name and new value. Setting a property by passing a string toattr
will attempt to set a deep property and will throw an error.See also
For information on the events that are fired on property changes and how to listen for those events, see bind.