RXJ和可选的内部查询

发布于 2025-01-18 10:22:33 字数 964 浏览 1 评论 0原文

我有一种网络表单,有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2025-01-25 10:22:33

我说的是,只需检查一次

import { from, of } from 'rxjs';
import { concatMap } from 'rxjs/operators';

const master$ = from([1, 2, 3, 4]);
const child$ = of(20);
// const child$ = () => console.log("child called");

master$
  .pipe(
    concatMap((masterData) => {
      if (masterData % 2 === 0) {
        child$;
        // child$(); // if you want to check,child is being  called or not
      }
      return of(masterData);
    })
  )
  .subscribe(console.log);

I was saying this,just check once

import { from, of } from 'rxjs';
import { concatMap } from 'rxjs/operators';

const master$ = from([1, 2, 3, 4]);
const child$ = of(20);
// const child$ = () => console.log("child called");

master$
  .pipe(
    concatMap((masterData) => {
      if (masterData % 2 === 0) {
        child$;
        // child$(); // if you want to check,child is being  called or not
      }
      return of(masterData);
    })
  )
  .subscribe(console.log);
想挽留 2025-01-25 10:22:33

该解决方案正是我所需要的

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)

This solution does exactly what I need

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文