2016-05-13 62 views
0

我有一个Aurelia网络应用程序,通过名称搜索艺术家,然后返回艺术家的所有艺术作品。由艺术家搜索返回的JSON类似于这样:Aurelia - 将来自两个API调用的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

请不要张贴代码的截图,这没有任何意义。 – Tomalak

+0

我看到有关不张贴网址的常见评论,因为它可能会变成一个死链接有一天...所以这是它? – smoore4

+0

很明显,邮政编码为文本。 – Tomalak

回答

2

Promise.all() function

返回一个承诺,当迭代参数中的所有承诺都解决了,或拒绝了第一个被拒绝的承诺的原因。

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) 
    ]); 
} 

如果jQuery的实际执行您的Ajax调用(即,如果this.http.fetch是一个包装周围$.ajax),你可以使用jQuery的jquery.when(),它满足同样的目的,但让你独立的the browser's native support for the Promise API

function getBySearchTerms(searchCreator) { 
    return $.when(
     this.getByConstituentId(5350), 
     this.getByConstituentId(5349) 
    ); 
} 

请注意不同的调用约定。 $.when需要一个参数列表,而Promise.all()需要一个可迭代(如数组)。

您可以通过.apply()使用$.when以及数组参数。

function getBySearchTerms(searchCreator) { 
    return $.when.apply($, [ 
     this.getByConstituentId(5350), 
     this.getByConstituentId(5349) 
    ]); 
} 
+0

这看起来很“有前途”。现在进行测试并没有发现错误,但也没有得到预期的结果。将很快回复你。 – smoore4

+1

'getBySearchTerms'返回另一个承诺。相应地使用返回值。 – Tomalak