在这个要点的app.js
中:https://gist.run/?id=acf8253329939b2e046cd0e3394351fe
SuggestionService
如何运作?
export class SuggestionService {
constructor(model, countries) {
this.model = model;
this.countryIndex = countries;
this.countries = Object.keys(countries);
}
country = {
suggest: value => {
if (value === '') {
return Promise.resolve([]);
}
value = value.toLowerCase();
const suggestions = this.countries
.filter(x => x.toLowerCase().indexOf(value) === 0)
.sort();
return Promise.resolve(suggestions);
},
getName: suggestion => suggestion
};
city = {
suggest: value => {
if (value === '' || this.model.country === null) {
return Promise.resolve([]);
}
value = value.toLowerCase();
let suggestions = this.countryIndex[this.model.country]
.filter(x => x.toLowerCase().indexOf(value) === 0);
suggestions = suggestions.filter((x, i) => suggestions.indexOf(x) === i)
.sort();
return Promise.resolve(suggestions);
},
getName: suggestion => suggestion
};
}
我不明白行getName: suggestion => suggestion
,我认为它是从建议承诺中选择元素,这是正确的吗?这是如何工作的?
在app.html
中,此suggestion
属性用于绑定:
<span style="font-style: italic">${suggestion}</span>