javascript - Using Bacon.js to disable submit button while deferreds are "pending" -
i have form, has many image urls - back-end persists url strings , images uploaded directly s3. i'd use bacon.js streams handle disabling/enabling form's submit button while uploads in-progress.
i've tried various approaches (using stream of streams of bacon.frompromises, using stream of frompromise-d deferreds , bus of raw deferreds , trying manually diff two) haven't found solution a) works intended , b) feels i'm not fighting library.
this things stand, noted, submit button prematurely re-enabled.
function toresultstream(promise) { return bacon.frompromise(promise) } var deferreds = $('a').aseventstream('click', function (event) { event.preventdefault(); var deferred = $.deferred(); // simulate upload settimeout(function () { deferred.resolve(true); }, _.random(200, 1600)) settimeout(function () { deferred.rejectwith(false); }, _.random(200, 1600)) return deferred; }); deferreds.onvalue(function () { $('#submit').attr('disabled', true); }) // takes completed deferreds consideration var ongoingsearch = deferreds.flatmap(function (d) { return toresultstream(d); }) .maperror(true) .onvalue(function () { $('#submit').attr('disabled', false); });
*update
@mjs2600's answer enough nudge me towards solution.
here's ended doing:
var bus = new bacon.bus(); var deferreds = $('a').aseventstream('click', function (event) { // ... bus.push(-1); // ... }); var ongoingsearch = deferreds .flatmap(toresultstream) .maperror(1) .merge(bus) .scan(0, function (memo, n) { return memo + n; }) .onvalue(function (value) { var disabled = value < 0; $('#submit').attr('disabled', disabled); });
i know using buses frowned upon, if has suggestion how achieve same behavior streams, i'd see it.
i make stream , map -1 when make request , +1 when response. then, can run fold on stream , enable button when equals 0.
Comments
Post a Comment