我正在尝试找到一个可以填写多种搜索类型的搜索页面。然后,如果搜索类型被填入并作为参数传递给activate(),则进行WebApi调用。
如果我知道param为null / undefined,那么调用api似乎效率低下,但我不知道如何做这样的事情:
if (params.myparam === undefined)
{ don't include it in the promise }
else
{ include in the promise }
以下是我到目前为止的代码....
这一切都正常,但我最终将“undefined”作为搜索参数传递。这对我不起作用,因为实际上有一件名为“来自 Undefined 小说的八张系列中的模具制造者”的艺术作品,在这种情况下不应该出现。并且“未定义”需要是有效的搜索词。
activate(params) {
if(params.exhibitionID != null)
{
return this.apiData.getByExhibitionId(params.exhibitionID).then(exhibitions => { this.exhibitions = exhibitions.objects; if(exhibitions.objects.length > 0){ this.showExhibitions = "display"; this.exhibitionsCount = exhibitions.objects.length; this.exhibitionTitle = exhibitions.exhibitionTitle; } });
}
else
{
return Promise.all([
this.apiData.getBySearchDated(params.searchDated).then(dateds => { this.dateds = dateds; if(dateds.length > 0){ this.showDateds = "display"; this.datedsCount = dateds.length; } }),
this.apiData.getBySearchCreditline(params.searchCreditline).then(creditlines => { this.creditlines = creditlines; if(creditlines.length > 0){ this.showCreditlines = "display"; this.creditlinesCount = creditlines.length; } }),
this.apiData.getBySearchNumber(params.searchNumber).then(numbers => { this.numbers = numbers; if(numbers.length > 0){ this.showNumbers = "display"; this.numbersCount = numbers.length; } }),
this.apiData.getBySearchTitle(params.searchTitle).then(titles => { this.titles = titles; if(titles.length > 0){ this.showTitles = "display"; this.titlesCount = titles.length; } }),
this.apiData.getBySearchCreator(params.searchCreator).then(creators => { this.creators = creators; if(creators.length > 0){ this.showCreators = "display"; this.creatorsCount = creators.length; } })
]);
}
}
以下是其中一个api调用的样子。我放入了一个返回空json的hack,但是如果我知道没有结果,那么首先调用它似乎是正确的。有什么建议?谢谢!
API调用:
getBySearchTitle(searchTitle) {
// the hack
// if(searchTitle == undefined){ searchTitle = token;}
return this.http.fetch(baseUrl + "/objects/" + token + "&search=" + searchTitle + "&searchtype=title")
.then(response => response.json())
.then(response => {
if(response.length > 1000)
{
alert("Your search returns too many results (maximum 1000). Please refine your search and try again.");
this.router.navigateBack();
}
else{
return response.objects;
}
});
}
答案 0 :(得分:0)
您可以在构建Promise
之前检查参数是否存在。你的激活方法看起来像这样:
activate(params) {
if(params.exhibitionID != null)
{
return this.apiData.getByExhibitionId(params.exhibitionID).then(exhibitions => { this.exhibitions = exhibitions.objects; if(exhibitions.objects.length > 0){ this.showExhibitions = "display"; this.exhibitionsCount = exhibitions.objects.length; this.exhibitionTitle = exhibitions.exhibitionTitle; } });
}
else
{
return Promise.all([
!params.searchDated || this.apiData.getBySearchDated(params.searchDated).then(dateds => { this.dateds = dateds; if(dateds.length > 0){ this.showDateds = "display"; this.datedsCount = dateds.length; } }),
!params.searchCreditline || this.apiData.getBySearchCreditline(params.searchCreditline).then(creditlines => { this.creditlines = creditlines; if(creditlines.length > 0){ this.showCreditlines = "display"; this.creditlinesCount = creditlines.length; } }),
!params.searchNumber || this.apiData.getBySearchNumber(params.searchNumber).then(numbers => { this.numbers = numbers; if(numbers.length > 0){ this.showNumbers = "display"; this.numbersCount = numbers.length; } }),
!params.searchTitle || this.apiData.getBySearchTitle(params.searchTitle).then(titles => { this.titles = titles; if(titles.length > 0){ this.showTitles = "display"; this.titlesCount = titles.length; } }),
!params.searchCreator || this.apiData.getBySearchCreator(params.searchCreator).then(creators => { this.creators = creators; if(creators.length > 0){ this.showCreators = "display"; this.creatorsCount = creators.length; } })
]);
}
}