COMPUTE-ATTR
{can.compute(getterSetter, context)}
Specify an attribute that is computed from other attributes.
{can.compute(getterSetter, context)}
Specify an attribute that is computed from other attributes.
Use
When extending can.Map, if a prototype property is a can.compute it will setup that compute to behave like a normal attribute. This means that it can be read and written to with attr and bound to with bind.
The following example makes a
fullName
attribute onPerson
maps:Getter / Setter computes
A compute's setter will be called if attr is used to set the compute-property's value.
The following makes
fullName
able to setfirst
andlast
:Alternatives
can.Mustache and can.EJS will automatically convert any function read in the template to a can.compute. So, simply having a fullName function like:
Will already be live-bound if read in a template like:
The setter plugin can also provide similar functionality as Getter/Setter computes.