ant-design-vue/components/vc-select/demo/tbFetchSuggest.js

36 lines
725 B
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import jsonp from 'jsonp';
import querystring from 'querystring';
let timeout;
let currentValue;
2018-02-11 10:04:31 +00:00
2019-01-12 03:33:27 +00:00
export function fetch(value, callback) {
2018-02-11 10:04:31 +00:00
if (timeout) {
2019-01-12 03:33:27 +00:00
clearTimeout(timeout);
timeout = null;
2018-02-11 10:04:31 +00:00
}
2019-01-12 03:33:27 +00:00
currentValue = value;
2018-02-11 10:04:31 +00:00
2019-01-12 03:33:27 +00:00
function fake() {
2018-02-11 10:04:31 +00:00
const str = querystring.encode({
code: 'utf-8',
q: value,
2019-01-12 03:33:27 +00:00
});
/*eslint-disable */
jsonp(`http://suggest.taobao.com/sug?${str}`, (err, d) => {
2018-02-11 10:04:31 +00:00
if (currentValue === value) {
2019-01-12 03:33:27 +00:00
const result = d.result;
const data = [];
result.forEach(r => {
2018-02-11 10:04:31 +00:00
data.push({
value: r[0],
text: r[0],
2019-01-12 03:33:27 +00:00
});
});
callback(data);
2018-02-11 10:04:31 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-02-11 10:04:31 +00:00
}
2019-01-12 03:33:27 +00:00
timeout = setTimeout(fake, 300);
2018-02-11 10:04:31 +00:00
}