Frage

I'm new to Bacon.js and I'm trying a lot to figure out how it works. So, is there a way to use variable, for example the hash from the URL, as if it would be an EventStream of onVariableChange events. Sorry if what I'm writing is awkward.

Here is my Code:

var hash =  window.location.hash.slice(1, window.location.hash.length);

newdata = hash.asEventStream("change").onValue( function(){**do_something_with_new_hash_value();**
} );
War es hilfreich?

Lösung

asEventStream creates an Bacon.js EventStream from a jQuery event. So you can you only apply it to a jQuery object, not to a JavaScript variable. So in your case you can use the jQuery/HTML5 event hashchange to create an EventStream from its changes:

$(window).asEventStream('hashchange')
         .map(function () { return window.location.hash; });

There is no way to convert a variable by itsself to a EventStream or Property, because there needs to be a way to notify about changes, which normal variables do not supply. However, you can use a Bacon.Bus to create an EventStream or Property, given that you call push whenever the variables changes.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top