SteveSanderson/knockout — Issue #326: Should be able to read an observable without creating a subscription
Author: mbest
Created: 2012-02-13 07:54:15+00:00
State: open

There are times when it's useful to read an observable without creating a subscription. Here's a simple example:

```javascript
var counter = ko.observable(0), somethingToWatch = ko.observable();
var somethingDoubled = ko.computed(function() {
counter(counter()+1);
return somethingToWatch() * 2;
});
```

The call to `counter()` creates a subscription, but since we don't use the value except to write to `counter`, we shouldn't have a subscription. In fact this code will cause an stack overflow because writing to `counter` will trigger re-running the function.

In #225, I suggested adding a `_latestValue` property to observables so the value could be accessed when inspecting the observable in a debugger. This could be extended by exporting the property, and then it'd be possible to read the value directly without triggering a subscription. Writing to the property would not be supported, of course.

Another solution would be to add a function to observables like `getWithoutDependency` that returns the value.

Outside of modifying Knockout, a way to solve the example above is to update `counter` asynchronously in a setTimeout call: `setTimeout(function() { counter(counter()+1); }, 0);`