evented attributes with automatic dependencies
Create a new attr with value set to val
attr = require('attr')
name = attr('Homer Simpson')
Read a value
name() // => 'Homer Simpson'
Write a value
name('Bart Simpson')
name() // => 'Bart Simpson'
- Emits "change" event with
(value, previousValue)
.
name.on('change', function(new_name, old_name) {
console.log('my name changed from', old_name, 'to', new_name)
})
Has the same API, except function is passed in and is run to determine the initial value. (has no setter)
fullName = attr(function() {
return firstName() + ' ' + surName()
})
fullName() // => 'Homer Simpson'
fullName.dependencies // => [ firstName, surName ]
Dependencies on other simple (non-computed) are automatically calculated unless set explicitly via a second argument as in:
fullName = attr(function() {
return firstName() + ' ' + surName()
}, [firstName])
fullName.dependencies // => [ firstName ]
Flips truthy to false and falsey to true
Increments value by 1 or val
Binds fn to the change
event
force a change event
contains the current value. Use to get value without running the getter (most useful for computed attr)
contains the last value
Contains a list of dependencies (this will be null for simple attr)
By default function values are assumed to be computed. This behaviour can be turned off by setting autocompute.
Explicitly creates a computed attr - this is only useful if autocompute is turned off
Calculates a list of the simple (non-computed) attributes called by running this function
make && open test/index.html
MIT