我有一个Aurelia网络应用程序,可以按名称搜索艺术家,然后返回艺术家的所有艺术作品。艺术家搜索返回的json看起来类似于:
{
"source": "Museum",
"language": "EN",
"resultsCount": 1,
"objects": [
{
"objectNumber": "125.1988",
"objectID": 981,
"title": "Governor's Palace (Raj Bhaven) project, Chandigarh, India",
"displayName": "Le Corbusier (Charles-Édouard Jeanneret)",
"alphaSort": "Le Corbusier (Charles-Édouard Jeanneret)",
"artistID": 3426,
"displayDate": "French, born Switzerland. 1887–1965",
"dated": "1951–1976",
"dateBegin": 1951,
"dateEnd": 1976,
"medium": "Wood, cardboard, and plexiglass",
"dimensions": "33 x 71 1/4 x 65\" (83.8 x 181 x 165.1 cm)",
"department": "Architecture & Design",
"classification": "A&D Architectural Model",
"onView": 0,
"provenance": "",
etc....
完整的json在这里:
https://github.com/smoore4moma/tms-api/blob/master/object.json
我遇到的问题是,艺术家搜索可以返回多个具有相似名称的艺术家(例如“Serra”返回Richard Serra和Daniel Serra-Badué)。因此,我可以循环搜索并调用API两次以获得艺术作品,但我还没有找到一种方法来组合两个不同的API调用,而不是Aurelia。这是我想要做的代码片段。我已经尝试过jQuery,.extend,.concat,.push,创建数组,字典....我只是卡住了。
getByConstituentId(id) {
return this.http.fetch(baseUrl + "/artists/" + id + token)
.then(response => response.json())
.then(response => {
return response.objects;
});
}
// This is not the actual looping code. But the idea is to combine these two function calls into one result and return that.
getBySearchTerms(searchCreator) {
let obj0 = this.getByConstituentId(5350);
let obj1 = this.getByConstituentId(5349);
return ??? // This is where I am looking for help.
// just for ex. "return this.getByConstituentId(5349);"
// works just fine
}
关于如何做到这一点的任何建议?否则,我将构建一个采用艺术家ID数组的API方法,但我宁愿使用简单的API方法并在需要时组合json。感谢。
后续
按照@Tomalak的建议,这最终会起作用:
return Promise.all([
this.getByConstituentId(5349),
this.getByConstituentId(5350)])
.then(response => {
return response[0].concat(response[1]);
});
答案 0 :(得分:2)
[...]返回一个promise,它在iterable参数中的所有promise都已解析时解析,或者拒绝第一个传递的拒绝承诺的原因。
(defn only-those-actors? [db movie actors]
(->> (datoms db :eavt movie :film/cast) seq
(every? (fn [[_ _ actor]]
(contains? actors actor)))
))
(defn find-movies-with-exact-cast [db actors-names]
(let [actors (set (d/q '[:find [?actor ...] :in $ [?name ...] ?only-those-actors :where
[?actor :actor/name ?name]]
db actors-names))
query {:find '[[?movie ...]]
:in '[$ ?actors ?db]
:where
(concat
(for [actor actors]
['?movie :film/cast actor])
[['(only-those-actors? ?db ?movie ?actors)]])}]
(d/q query db actors db only-those-actors?)))
如果jQuery实际上执行了Ajax调用(即如果function getByConstituentId(id) {
return this.http.fetch(baseUrl + "/artists/" + id + token)
.then(response => response.json().objects);
};
function getBySearchTerms(searchCreator) {
return Promise.all([
this.getByConstituentId(5350),
this.getByConstituentId(5349)
]);
}
是this.http.fetch
的包装器),则可以使用jQuery' s jquery.when()
,它可以实现相同的目的,但会让你独立于the browser's native support for the Promise API。
$.ajax
请注意不同的呼叫约定。 function getBySearchTerms(searchCreator) {
return $.when(
this.getByConstituentId(5350),
this.getByConstituentId(5349)
);
}
期望参数列表,而$.when
期望迭代(如数组)。
您可以通过Promise.all()
使用$.when
数组参数。
.apply()