can-simple-map
Version:
A performant live-bound map
69 lines (46 loc) • 1.65 kB
Markdown
set properties on a SimpleMap.
`map.attr(key)`
Reads a property from this `SimpleMap`.
{String} key The property to read.
{*} The value assigned to _key_.
`map.attr(key, value)`
Assigns _value_ to a property on this `SimpleMap` called _key_.
{String} key The property to set.
{*} value The value to assign to _key_.
{can.SimpleMap} This SimpleMap, for chaining.
`map.attr(obj)`
Assigns each value in _obj_ to a property on this `SimpleMap` named after the
corresponding key in _obj_, effectively merging _obj_ into the SimpleMap.
{Object} obj A collection of key-value pairs to set.
If any properties already exist on the `SimpleMap`, they will be overwritten.
{can.SimpleMap} this SimpleMap, for chaining
## Use
`attr` gets or sets properties on the `SimpleMap` it's called on. Here's a tour through how all of its forms work:
```
var map = new SimpleMap({ age: 29 });
// get a property:
foo.attr('age'); // 29
// set a property:
foo.attr('age', 30);
foo.attr('age'); // 30
// set and merge multiple properties:
foo.attr({
first: 'Kevin',
last: 'Phillips'
});
foo.attr('age'); // 30
foo.attr('first'); // 'Kevin'
foo.attr('last'); // 'Phillips'
```
When properties are changed using attr, the `SimpleMap` will emit events. Events can be listened to using [can-event.on] or [can-event.bind].
```
var map = new SimpleMap({ age: 29 });
map.on('age', function(ev, newVal, oldVal) {
newVal; // 30
oldVal; // 29
});
map.attr('age', 30);
```
can-simple-map.prototype.attr attr
can-simple-map.prototype
Get or