Skip to content

Commit

Permalink
fix computes that use other computes
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Sloan committed Oct 2, 2012
1 parent 5ad2563 commit 83a48e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions observe/compute/compute.js
Expand Up @@ -266,6 +266,9 @@ steal('can/util', function(can) {
if(value === undefined){
// we are reading
if(computedData){
if(bindings && can.Observe.__reading) {
can.Observe.__reading(computed,'change');
}
return computedData.value;
} else {
return getterSetter.call(context || this)
Expand Down
45 changes: 45 additions & 0 deletions observe/compute/compute_test.js
Expand Up @@ -71,3 +71,48 @@ test("setter compute", function(){
computed(75);

})

test("compute a compute", function() {
var project = new can.Observe({
progress: 0.5
});

var percent = can.compute(function(val){
if(val) {
project.attr('progress', val / 100);
} else {
return parseInt( project.attr('progress') * 100, 10);
}
});

equals(percent(),50,'percent starts right');
percent.bind('change',function() {
// noop
});

var fraction = can.compute(function(val) {
if(val) {
percent(parseInt(val.split('/')[0],10));
} else {
return percent() + '/100';
}
});

fraction.bind('change',function() {
// noop
});

equals(fraction(),'50/100','fraction starts right');

percent(25);

equals(percent(),25);
equals(project.attr('progress'),0.25,'progress updated');
equals(fraction(),'25/100','fraction updated');

fraction('15/100');

equals(fraction(),'15/100');
equals(project.attr('progress'),0.15,'progress updated');
equals(percent(),15,'% updated');
});

0 comments on commit 83a48e7

Please sign in to comment.