[英]Functional Javascript BaconJS, how can I push more values to an event stream?
I'm attempting to create a stack of AJAX responses in BaconJS. 我试图在BaconJS中创建一堆AJAX响应。 That processes them in a first in first out fashion, but each 'out' event should wait for user input. 它以先进先出的方式处理它们,但每个'out'事件应该等待用户输入。
This is where I'm at now: Live JSBin 这就是我现在所处的位置: 直播JSBin
var pages = Bacon.fromArray([1,2,3])
var next = $("#next").asEventStream('click').map(true);
pages.flatMapConcat(asyncFunction).zip(next).log("responses")
function asyncFunction(page) {
// Simulating something like an AJAX request
return Bacon.later(1000 + (Math.random() * 3000), "Page "+ page)
}
Currently this synchronously outputs an event from the pages
EventStream
each time that #next
is clicked, which the behavior I want. 目前, #next
单击EventStream
时,这将同步从EventStream
pages
输出一个事件,这是我想要的行为。
However , I am unable to figure out how to push more values to the pages
EventStream
. 但是 ,我无法弄清楚如何将更多值推送到EventStream
pages
。 I have attempted to replace the pages EventStream with a Bus
, and pushing values like this (which doesn't work). 我试图用Bus
替换EventStream页面,并推送像这样的值(这不起作用)。
var pages = new Bacon.Bus()
pages.push("value")
How do I push more values to an EventStream
? 如何将更多值推送到EventStream
?
I know this is an OLD post, but bus would work. 我知道这是一个老帖子,但公共汽车会工作。 Just push the number (you had an array of numbers before) into it: 只需将数字(之前有一个数组)推入其中:
var pages = new Bacon.Bus();
// Bacon.fromArray([1,2,3])
var next = $("#next").asEventStream('click').map(true);
pages.flatMapConcat(asyncFunction).zip(next).log("responses")
function asyncFunction(page) {
// Simulating something like an AJAX request
return Bacon.later(1000 + (Math.random() * 3000), "Page "+ page)
}
pages.push(1);
pages.push(2);
pages.push(3);
I cloned your jsbin and changed it to use bus 我克隆了你的jsbin并将其更改为使用总线
As mentioned previously, you could stream the source of the page values using something like fromEvent or from fromBinder 如前所述,您可以使用fromEvent或fromBinder等流式传输页面值的来源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.