Aurelia - 如果 Promise.all 中的参数
Aurelia - If params in Promise.all
我正在尝试制作一个可以填写多种搜索类型的搜索页面。然后,如果填写了搜索类型并将其作为参数传递给 activate(),则会调用 WebApi。
如果我知道参数是 null/undefined,调用 api 似乎效率低下,但我不确定如何做这样的事情:
if (params.myparam === undefined)
{ don't include it in the promise }
else
{ include in the promise }
下面是我目前的代码....
一切正常,但我最终将 "undefined" 作为搜索参数传递。这对我不起作用,因为实际上有一件名为 "The Die Maker from the series Eight Sheets from an Undefined Novel" 的艺术品,在这种情况下不应出现。 "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 调用之一的样子。我输入了一个 returns 为空 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;
}
});
}
你可以在构造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; } })
]);
}
}
我正在尝试制作一个可以填写多种搜索类型的搜索页面。然后,如果填写了搜索类型并将其作为参数传递给 activate(),则会调用 WebApi。
如果我知道参数是 null/undefined,调用 api 似乎效率低下,但我不确定如何做这样的事情:
if (params.myparam === undefined)
{ don't include it in the promise }
else
{ include in the promise }
下面是我目前的代码....
一切正常,但我最终将 "undefined" 作为搜索参数传递。这对我不起作用,因为实际上有一件名为 "The Die Maker from the series Eight Sheets from an Undefined Novel" 的艺术品,在这种情况下不应出现。 "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 调用之一的样子。我输入了一个 returns 为空 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;
}
});
}
你可以在构造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; } })
]);
}
}