can/compute/get_value_and_bind
This module:
Exports a function that calls an arbitrary function and binds to any observables that
function reads. When any of those observables change, a callback function is called.
And …
Adds two main methods to can:
- can.__observe - All other observes call this method to be visible to computed functions.
- can.__notObserve - Returns a function that can not be observed.
steal("can/util", function(can){
function ObservedInfo(func, context, compute){
this.newObserved = {};
this.oldObserved = null;
this.func = func;
this.context = context;
this.compute = compute;
this.onDependencyChange = can.proxy(this.onDependencyChange, this);
this.depth = null;
this.childDepths = {};
this.ignore = 0;
this.inBatch = false;
this.ready = false;
compute.observedInfo = this;
this.setReady = can.proxy(this._setReady, this);
}
can.simpleExtend(ObservedInfo.prototype,{
getPrimaryDepth: function() {
return this.compute._primaryDepth;
},
_setReady: function(){
this.ready = true;
},
getDepth: function(){
if(this.depth !== null) {
return this.depth;
} else {
return (this.depth = this._getDepth());
}
},
_getDepth: function(){
var max = 0,
childDepths = this.childDepths;
for(var cid in childDepths) {
if(childDepths[cid] > max) {
max = childDepths[cid];
}
}
return max + 1;
},
addEdge: function(objEv){
objEv.obj.bind(objEv.event, this.onDependencyChange);
if(objEv.obj.observedInfo) {
this.childDepths[objEv.obj._cid] = objEv.obj.observedInfo.getDepth();
this.depth = null;
}
},
removeEdge: function(objEv){
objEv.obj.unbind(objEv.event, this.onDependencyChange);
if(objEv.obj.observedInfo) {
delete this.childDepths[objEv.obj._cid];
this.depth = null;
}
},
dependencyChange: function(ev){
if(this.bound && this.ready) {
if(ev.batchNum !== undefined) {