CanJS - Compute

canjs - done reading

What is the purpose of can.compute?

Create an observable value. Create a compute that derives its value from can.Maps and other can.computes. can.compute lets you make an observable value. Computes are similar to Observes, but they represent a single value rather than a collection of values. can.compute returns a compute function that can be called to read and optionally update the compute's value.

It's also possible to derive a compute's value from other computes, can.Maps, and can.Lists. When the derived values change, the compute's value will be automatically updated.

How can we use a simple compute?

The simplest way to use a compute is to have it store a single value, and to set it when that value needs to change:

var tally = can.compute(12);
tally(); // 12

tally.bind("change",function(ev, newVal, oldVal){

tally(); // 13

Any value can be observed. The following creates a compute that holds an object and then changes it to an array.

var data = can.compute({name: "Justin"})
data([{description: "Learn Computes"}])
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License