最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RxJS split observable sequence in multiple output - Stack Overflow

programmeradmin1浏览0评论

Is it possible to split a single observable flux in multiple other observables?

My use case is a form that a user can submit. The submit action is handled in an observable, and on this action, there's a validator listening.

submitAction.forEach(validate)

The thing is I want to bind actions to either the success or the failure of the validator check.

validationFailure.forEach(outputErrors)
validationSuccess.forEach(goToPage)

I'm not sure how similar cases are handled in reactive programming - it may be that splitting the observable is just not the right solution for handling this kind of issue.

Anyway, how would you handle a similar case?

Is it possible to split a single observable flux in multiple other observables?

My use case is a form that a user can submit. The submit action is handled in an observable, and on this action, there's a validator listening.

submitAction.forEach(validate)

The thing is I want to bind actions to either the success or the failure of the validator check.

validationFailure.forEach(outputErrors)
validationSuccess.forEach(goToPage)

I'm not sure how similar cases are handled in reactive programming - it may be that splitting the observable is just not the right solution for handling this kind of issue.

Anyway, how would you handle a similar case?

Share Improve this question asked Jul 6, 2014 at 7:27 Simon BoudriasSimon Boudrias 44.6k16 gold badges108 silver badges136 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

Can you just use map and filter, possibly with share to avoid repeatedly executing the validation logic?

var submitAction = // some Rx.Observable
var validationResult = submitAction.map(validate).share();
var success = validationResult.filter(function (r) { return !!r; });
var failure = validationResult.filter(function (r) { return !r; });

success.subscribe(goToPage);
failure.subscribe(outputErrors);
发布评论

评论列表(0)

  1. 暂无评论