can-symbol
Version:
Well known symbols used to detail how to operate on different objects
34 lines (28 loc) • 1 kB
Markdown
function(String, function(*))} can-symbol/symbols/onKeyValue can.onKeyValue
can-symbol/symbols/observe
Defines how observable values can be listened to on a type.
`@@can.onKeyValue( key, handler(newValue) )`
The `@@@@can.onKeyValue` symbol points to a function that registers
`handler` to be called back with the new value of `key` when `key`
changes.
```
var obj = {
handlers: {},
setKeyValue: function(key, value){
this[key] = value;
var self = this;
obj.handlers[key].forEach(function(handler){
handler.call(self, value);
});
}
};
obj[canSymbol.for("can.onKeyValue")] = function(key, handler){
if(!obj.handlers[key]) {
obj.handlers[key] = [];
}
obj.handlers[key].push(handler);
}
```
{Object} any Map-like object with named properties
{String} key the string key to bind on changes to.
{function(this:*, *)} handler(newValue) The handler must be called back with `this` as the instance of the observable.
{