can.Map
can/map
inherits: can.Construct
Create observable objects.
new can.Map([props])
Creates a new instance of can.Map.
Parameters
-
props
{Object}
OptionalProperties and values to seed the Observe with.
Returns
{can.Map}
An instance of can.Map
with the properties from props.
can.Map([name,] [staticProperties,] instanceProperties)
Creates a new extended constructor function.
This is deprecated. In CanJS 1.2, by default, calling the constructor function
without new
will create a new
instance. Use can.Map.extend
instead of calling the constructor to extend.
can.Map
provides a way for you to listen for and keep track of changes to objects. When you use the getters and setters provided bycan.Map
, events are fired that you can react to.can.Map
also has support for working with deep properties. Observable arrays are also available withcan.List
, which is based oncan.Map
.Working with Observes
To create an Observe, use
new can.Map([props])
. This will return a copy ofprops
that emits events when its properties are changed withattr
.You can read the values of properties on Observes directly, but you should never set them directly. You can also read property values using
attr
. Usually, you will want to do this when creating acan.compute
or when live-binding properties in an EJS template. (If you are using Mustache, you don't need to useattr
.)Find out more about manipulating properties of Observes under attr and removeAttr.
Listening to changes
The real power of observable objects comes from being able to react to properties being added, set, and removed. Observes emit events when properties are changed that you can bind to.
can.Map
has two types of events that fire due to changes on an Observe:For more detail on how to use these events, see bind and unbind. There is also a plugin called delegate that makes binding to specific types of events easier: