can-map
Observable Objects
API
can-map
Create observable objects.
new Map([props])
Creates a new instance of can.Map.
- props
{Object}
:
Properties and values to seed the Observe with.
- returns
{can.Map}
:
An instance of can.Map
with the properties from props.
Map.extend([name,] [staticProperties,] instanceProperties)
Creates a new extended constructor function.
map.attr()
Gets a collection of all the properties in this Map
.
- returns
{Object}
:
an object with all the properties in this Map
.
map.attr(key)
Reads a property from this Map
.
- key
{String}
:
the property to read
- returns
{*}
:
the value assigned to key.
map.attr(key, value)
Assigns value to a property on this Map
called key.
- key
{String}
:
the property to set - 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 Map
named after the
corresponding key in obj, effectively merging obj into the Map.
-
obj {Object}
:
a collection of key-value pairs to set.
If any properties already exist on the Map
, they will be overwritten.
-
removeOthers {bool}
:
whether to remove keys not present in obj.
To remove keys without setting other keys, use [can.Map::removeAttr removeAttr]
.
- returns
{can.Map}
:
this Map, for chaining
map.bind(eventType, handler)
- eventType
{String}
:
the type of event to bind this handler to - handler
{function}
:
the handler to be called when this type of event fires
The signature of the handler depends on the type of event being bound. See below
for details.
- returns
{can.Map}
:
this Map, for chaining
map.compute(attrName)
- attrName
{String}
:
the property to bind to
- returns
{can-compute}
:
a [can-compute] bound to attrName
DEFAULT-ATTR {*}
Specify a default property and value.
*
A value of any type other than a function that will
be set as the DEFAULT-ATTR
attribute's value.
map.each( callback(item, propName ) )
each
iterates through the Map, calling a function
for each property value and key.
- callback
{function(item, propName)}
:
the function to call for each property
The value and key of each property will be passed as the first and second
arguments, respectively, to the callback. If the callback returns false,
the loop will stop.
- returns
{can-map}
:
this Map, for chaining
map.removeAttr(attrName)
- attrName
{String}
:
the name of the property to remove
- returns
{*}
:
the value of the property that was removed
map.serialize()
Get the serialized Object form of the map. Serialized
data is typically used to send back to a server.
o.serialize() //-> { name: 'Justin' }
Serialize currently returns the same data
as [can.Map.prototype.attrs]. However, in future
versions, serialize will be able to return serialized
data similar to [can.Model]. The following will work:
new Map({time: new Date()})
.serialize() //-> { time: 1319666613663 }
- returns
{Object}
:
a JavaScript Object that can be
serialized with JSON.stringify
or other methods.
map.unbind(eventType[, handler])
- eventType
{String}
:
the type of event to unbind, exactly as passed to bind
- handler
{function}
:
the handler to unbind
var map = new Map({ a: 1 });
function log(){
console.log("val", map.attr("a");
}
map.bind("change", log);
map.attr("a", 2);
map.unbind("change", log);
Map.keys(map)
var people = new Map({
a: 'Alice',
b: 'Bob',
e: 'Eve'
});
Map.keys(people);
- map
{can-map}
:
the Map
to get the keys from
- returns
{Array}
:
array An array containing the keys from map.
Contributing
Making a Build
To make a build of the distributables into dist/
in the cloned repository run
npm install
node build
Running the tests
Tests can run in the browser by opening a webserver and visiting the test.html
page.
Automated tests that run the tests from the command line in Firefox can be run with
npm test