This code:
var a = Observable(1, 2, 3);
console.log(a.toString());
var b = a.map(function (c) {
console.log(c);
return Math.sqrt(c);
});
console.log(b.toString());
is giving me this log:
LOG: (observable) 1,2,3
LOG: (observable)
So b
is correctly set as an Observable, but it’s empty. The logging inside the map function is giving nothing (although from the docs, it’s expected). It’s either a bug or the documentation is missing something. Same thing with where()
.
From what I understand, it’s because b
is not “consumed” ie used by the interface. How can I force it to be consumed? The example from the Observable doc is unclear to me (“If you run into this problem, you can manually add a subscriber for debugging purposes, as described here.”)
Hi!
You can achieve this by using addSubscriber to the b observable:
var a = Observable(1, 2, 3);
console.log(a.toString());
var b = a.map(function (c) {
console.log(c);
var ret = Math.sqrt(c);
console.log(ret);
return ret;
});
console.log(b.toString());
b.addSubscriber(function(x){});
However, while adding subscribers that do “nothing” may be suitable for debugging it isn’t necessarily what you want in the final app.
To make sure the observable is evaluated (consumed) you simply have to make sure it is actually used by the UI, by data binding it to some UI component. (Either directly or through a dependency chain where observableA is derived from observableB etc etc…)
OK I thought the console_log
had to be put in the subscriber function. This is all clearer now, thanks very much. Maybe including this example with the addSubscriber
to the doc could help!