RXJ和可选的内部查询
我有一种网络表单,有2个部分(主孩子和可选的孩子),并希望保存
第一个调用的表单值是主数据。呼叫结果用作第二个呼叫的参数。但是在某些情况下,我们不需要保存儿童部分。
在“订阅”部分保存的结尾,我想从第一个呼叫结果中显示一个字段的“成功”消息。我的意思是我们可以打一个或两个电话,但是当他们两个完成
主数据调用 - &gt时,显示成功消息。儿童数据调用(可选) - >成功消息
如何以适当的方式使用RXJ实施行为?
加法:
根据评论,我在这里创建了一个示例: https://rxjs.rxplayground.com/
var master = from([1,2])
var child = of(10)
master.pipe(concatMap(m=>(m==1)?child:EMPTY)).subscribe(res=>console.log(res))
(显示10但应为1,2)
尝试更改“主”值。在任何情况下,日志输出都应匹配主人。同样,避免“(m)”内部的“ condmap call
”的另一种补充是很高兴的:
我尝试过,但不确定它是最好的解决方案
var master = from([1,2]);
var child = of("*");
master.pipe(
mergeMap(m => (m === 1 ? child.pipe(mapTo(m)) : of(m)))
).subscribe(console.log);
(显示1,2)< /em>
I have a kind of web form which has 2 parts (master and optional child) and want to save the form values
A first call is for master data. The call result is used as a parameter for second call. But in some cases we don't need to save child part.
In the end of saving in the subscribe section I want to show "Success" message with one field from the first call result. I mean we can have one or two calls but show the success message when both of them completed
Master Data Call -> Child Data Call (optional) -> Success message
How to implement the behaviour using RxJS in a proper way?
Addition:
Based on comments I created an example here:
https://rxjs.rxplayground.com/
var master = from([1,2])
var child = of(10)
master.pipe(concatMap(m=>(m==1)?child:EMPTY)).subscribe(res=>console.log(res))
(shows 10 but should be 1,2)
Try to change "master" value. In any case log output should match the master. Also it would be nice to avoid "of(m)" inside of concatMap call
One more addition:
I tried this but not sure that it is the best solution
var master = from([1,2]);
var child = of("*");
master.pipe(
mergeMap(m => (m === 1 ? child.pipe(mapTo(m)) : of(m)))
).subscribe(console.log);
(shows 1,2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我说的是,只需检查一次
I was saying this,just check once
该解决方案正是我所需要的
(显示 1,2)
This solution does exactly what I need
(shows 1,2)