CanJS - Compute

canjs

http://canjs.com/docs/can.compute.html
http://canjs.com/docs/can.Map.prototype.compute.html
https://forum.javascriptmvc.com/topic/can-compute-crazies
http://blog.bitovi.com/weekly-widget-7-computes-and-sliders/
http://stackoverflow.com/questions/27697459/how-do-i-pass-a-can-compute-into-a-can-component
http://www.javascriptmvc.com/docs/can.compute.html
https://matthewphillips.info/posts/responsive-computes.html - 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){
    console.log(newVal,oldVal)
})

tally(13);
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