如何在Observable.bindCallback方法中使用RXJS选择器函数? [英] How do I use the RXJS selector function in the Observable.bindCallback method?
问题描述
我相信在使用Observable.bindCallback
方法时,要使两个参数正确地映射回回调,您必须使用选择器"功能,但是我找不到解释该操作方法的文档.我可能对选择器功能的功能有误解,但仍应记录在案.
I believe that to get two params properly mapped back to the callback when using Observable.bindCallback
method you have to use the "selector" function, but I cannot find documentation that explains how to do this. I may have a misunderstanding of what the selector function does, but it still should be documented.
http://reactivex.io/rxjs/class/es6/Observable.js〜Observable.html#static-method-bindCallback
function testLogin(username, password, callback){
// ...
callback(param1, param2);
}
function selectorFunction(???) {
// ???
}
function onTestLoginComplete(param1, param2) {
// ...
}
var observableFactory = Observable.bindCallback(testLogin, selectorFunction);
var observable = observableFactory('username', 'password');
observable.subscribe( (param1, param2) => onTestLoginComplete(param1, param2) );
推荐答案
subscribe
函数只能接受单个参数.因此,selector
函数是关于将多参数回调转换为单个元素.通常,这意味着您将把参数打包到一个对象中,然后可以在以后对其进行解构:
The subscribe
function can only ever take a single argument. So the selector
function is about converting a multiargument callback into a single element. In general this means that you will pack the arguments into an object, and you can destructure it later on:
function testLogin(username, password, callback){
// ...
callback(param1, param2);
}
//Convert this into a new object
function selectorFunction(param1, param2) {
return {param1, param2};
}
function onTestLoginComplete(param1, param2) {
// ...
}
var observableFactory = Observable.bindCallback(testLogin, selectorFunction);
var observable = observableFactory('username', 'password');
//De-structure the argument when it is passed to subscribe.
observable.subscribe( ({param1, param2}) => onTestLoginComplete(param1, param2) );
这篇关于如何在Observable.bindCallback方法中使用RXJS选择器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!