根据 Observable 元素的属性解析 Observables
Resolving Observables on properties of an Observable element
如果我有来自 api 的 Observable 项目列表,我们称它们为 "bars" 列表。
现在,对于每个 bar
,我都需要获取相关项目的列表 - 让我们称它们为 "foo"。事情是这样的 - api 允许您仅在提供关联的 bar
的 ID 时检索 foo
的列表。
那我该怎么做呢?我应该在 subscribe | forEach
中发出单独的请求吗?或者我可以创建 url 流并以某种方式将它们与 bars
流合并,然后那样做?
当应用于 UI 东西时,你可以使用 Rx 创造奇迹,不幸的是我很难用它来处理多个 ajax 请求。
你能给我举个例子吗?当你有一个 Observable 并且每一项你检索更多数据时?
例如,如果我尝试这样做:
Rx.Observable.fromArray([ { bar:1 }, { bar:2 }, { bar:3 } ])
.map((x)=> {
x.foo = Rx.Observable.fromPromise(grabFoo(x));
return x;
}).subscribe((x)=> console.log(x))
这是行不通的。那么我该怎么做呢?
我刚刚做了这个。我用的是 flatMap.
这是我的工作代码的编辑形式:
已更新评论并使用 Bar 和 Foo 并添加 listofbars()
// accumulates all bar s and foo s
var data = {bars: [], errors: []};
// return observable of a foo
var getFoo = function (Id, apiKey) {
return Rx.Observable.fromPromise($.ajax({
method: "GET",
url: config.Uri + "/foos/" + Id,
headers: { "Authorization": "Bearer " + apiKey },
})).share();
};
// return Observable of an array of bars
var getBars = function (apiKey) {
return Rx.Observable.fromPromise($.ajax({
method: "GET",
url: config.Uri + "/bars",
headers: { "Authorization": "Bearer " + apiKey },
})).share();
};
// flatten Observable of an array of bar s to Observable of bar s
var listOfBars = function (apiKey) {
return getBars(apiKey)
.flatMap(function (ep) { return (_a = Rx.Observable).of.apply(_a, ep); var _a; });
}
// accumulate foos into data
var getDataForFoo = function (data, bar, apiKey) {
return getFoo(bar.fooId, apiKey)
.map(function (foo) {
bar.foo = foo;
data.bars.push(bar);
return data;
})
.catch(function (ex) {
data.errors.push({ name: { Id: Id, name: Name }, error: ex });
return Rx.Observable.of(data);
});
};
var getDataForBars = function (data, apiKey) {
return listOfBars(apiKey)
.flatMap(function (bar) { return getDataForFoo(data, bar, apiKey); })
.catch(function (ex) {
data.errors.push({ names: { Id: Id }, error: ex });
return Rx.Observable.of(data);
});
};
这个 flatMap 接受条形数组并分别发出每个条形。
.flatMap(function (ep) { return (_a = Rx.Observable).of.apply(_a, ep); var _a; })
这个 flatMap 获取每个条并使用条中的 fooId 发出新的 Web 请求。
.flatMap(function (bar) { return getDataForFoo(data, bar, apiKey); })
上面的例子将 bar 和 foo 累积到一个数据模型中。您似乎在要求包含 foo 的 bar 流。鉴于以上内容,它看起来像:
var barswithfoo = listOfBars(apiKey)
.flatMap(bar => {
return getFoo(bar.fooId, apiKey)
.map(foo => {
bar.foo = foo;
return bar;
});
})
.subscribe();
您可以看到这与您发布的答案相似。 flatMap 是映射+合并。它期望映射到 return 一个 Observable 并立即订阅每个 returned.
的 observable
所以,为了让类似的东西真正起作用,我首先尝试了这个:
listOfbars.flatMap(x =>
getFoo(x).then(r => {
x.foo = r;
return x;
})
)
.subscribe(x => console.log(x))
它有效,虽然它非常慢(因为我认为它只在最后解决所有承诺),与此相比:
listOfbars.flatMap(x =>
Rx.Observable.fromPromise(getFoo(x))
.map(r => {
x.foo = r;
return x;
})
)
.subscribe(x => console.log(x))
看起来第二个代码段根本不应该起作用,令人惊讶的是它起作用了
如果我有来自 api 的 Observable 项目列表,我们称它们为 "bars" 列表。
现在,对于每个 bar
,我都需要获取相关项目的列表 - 让我们称它们为 "foo"。事情是这样的 - api 允许您仅在提供关联的 bar
的 ID 时检索 foo
的列表。
那我该怎么做呢?我应该在 subscribe | forEach
中发出单独的请求吗?或者我可以创建 url 流并以某种方式将它们与 bars
流合并,然后那样做?
当应用于 UI 东西时,你可以使用 Rx 创造奇迹,不幸的是我很难用它来处理多个 ajax 请求。
你能给我举个例子吗?当你有一个 Observable 并且每一项你检索更多数据时?
例如,如果我尝试这样做:
Rx.Observable.fromArray([ { bar:1 }, { bar:2 }, { bar:3 } ])
.map((x)=> {
x.foo = Rx.Observable.fromPromise(grabFoo(x));
return x;
}).subscribe((x)=> console.log(x))
这是行不通的。那么我该怎么做呢?
我刚刚做了这个。我用的是 flatMap.
这是我的工作代码的编辑形式:
已更新评论并使用 Bar 和 Foo 并添加 listofbars()
// accumulates all bar s and foo s
var data = {bars: [], errors: []};
// return observable of a foo
var getFoo = function (Id, apiKey) {
return Rx.Observable.fromPromise($.ajax({
method: "GET",
url: config.Uri + "/foos/" + Id,
headers: { "Authorization": "Bearer " + apiKey },
})).share();
};
// return Observable of an array of bars
var getBars = function (apiKey) {
return Rx.Observable.fromPromise($.ajax({
method: "GET",
url: config.Uri + "/bars",
headers: { "Authorization": "Bearer " + apiKey },
})).share();
};
// flatten Observable of an array of bar s to Observable of bar s
var listOfBars = function (apiKey) {
return getBars(apiKey)
.flatMap(function (ep) { return (_a = Rx.Observable).of.apply(_a, ep); var _a; });
}
// accumulate foos into data
var getDataForFoo = function (data, bar, apiKey) {
return getFoo(bar.fooId, apiKey)
.map(function (foo) {
bar.foo = foo;
data.bars.push(bar);
return data;
})
.catch(function (ex) {
data.errors.push({ name: { Id: Id, name: Name }, error: ex });
return Rx.Observable.of(data);
});
};
var getDataForBars = function (data, apiKey) {
return listOfBars(apiKey)
.flatMap(function (bar) { return getDataForFoo(data, bar, apiKey); })
.catch(function (ex) {
data.errors.push({ names: { Id: Id }, error: ex });
return Rx.Observable.of(data);
});
};
这个 flatMap 接受条形数组并分别发出每个条形。
.flatMap(function (ep) { return (_a = Rx.Observable).of.apply(_a, ep); var _a; })
这个 flatMap 获取每个条并使用条中的 fooId 发出新的 Web 请求。
.flatMap(function (bar) { return getDataForFoo(data, bar, apiKey); })
上面的例子将 bar 和 foo 累积到一个数据模型中。您似乎在要求包含 foo 的 bar 流。鉴于以上内容,它看起来像:
var barswithfoo = listOfBars(apiKey)
.flatMap(bar => {
return getFoo(bar.fooId, apiKey)
.map(foo => {
bar.foo = foo;
return bar;
});
})
.subscribe();
您可以看到这与您发布的答案相似。 flatMap 是映射+合并。它期望映射到 return 一个 Observable 并立即订阅每个 returned.
的 observable所以,为了让类似的东西真正起作用,我首先尝试了这个:
listOfbars.flatMap(x =>
getFoo(x).then(r => {
x.foo = r;
return x;
})
)
.subscribe(x => console.log(x))
它有效,虽然它非常慢(因为我认为它只在最后解决所有承诺),与此相比:
listOfbars.flatMap(x =>
Rx.Observable.fromPromise(getFoo(x))
.map(r => {
x.foo = r;
return x;
})
)
.subscribe(x => console.log(x))
看起来第二个代码段根本不应该起作用,令人惊讶的是它起作用了