Skip to content

Commit 9e20092

Browse files
committed
feat: allow to pass a custom fetch function (for auth, etc...)
1 parent 7b5eb1b commit 9e20092

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

dist/vue-typeahead.common.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ exports.default = {
5555

5656
this.loading = true;
5757

58-
this.fetch().then(function (response) {
58+
var suggestionPromise = this.fetchFunction ? this.fetchFunction(this.query) : this.fetchDefault(this.query);
59+
60+
suggestionPromise.then(function (data) {
5961
if (_this.query) {
60-
var data = response.data;
6162
data = _this.prepareResponseData ? _this.prepareResponseData(data) : data;
6263
_this.items = _this.limit ? data.slice(0, _this.limit) : data;
6364
_this.current = -1;
@@ -69,7 +70,7 @@ exports.default = {
6970
}
7071
});
7172
},
72-
fetch: function fetch() {
73+
fetchDefault: function fetchDefault(query) {
7374
if (!this.$http) {
7475
return _vue.util.warn('You need to install the `vue-resource` plugin', this);
7576
}
@@ -78,11 +79,13 @@ exports.default = {
7879
return _vue.util.warn('You need to set the `src` property', this);
7980
}
8081

81-
var src = this.queryParamName ? this.src : this.src + this.query;
82+
var src = this.queryParamName ? this.src : this.src + query;
8283

83-
var params = this.queryParamName ? (0, _assign2.default)((0, _defineProperty3.default)({}, this.queryParamName, this.query), this.data) : this.data;
84+
var params = this.queryParamName ? (0, _assign2.default)((0, _defineProperty3.default)({}, this.queryParamName, query), this.data) : this.data;
8485

85-
return this.$http.get(src, { params: params });
86+
return this.$http.get(src, { params: params }).then(function (response) {
87+
return response.data;
88+
});
8689
},
8790
reset: function reset() {
8891
this.items = [];

src/main.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,24 @@ export default {
3838

3939
this.loading = true
4040

41-
this.fetch().then((response) => {
42-
if (this.query) {
43-
let data = response.data
44-
data = this.prepareResponseData ? this.prepareResponseData(data) : data
45-
this.items = this.limit ? data.slice(0, this.limit) : data
46-
this.current = -1
47-
this.loading = false
48-
49-
if (this.selectFirst) {
50-
this.down()
41+
const suggestionPromise = this.fetchFunction ? this.fetchFunction(this.query) : this.fetchDefault(this.query);
42+
43+
suggestionPromise
44+
.then((data) => {
45+
if (this.query) {
46+
data = this.prepareResponseData ? this.prepareResponseData(data) : data
47+
this.items = this.limit ? data.slice(0, this.limit) : data
48+
this.current = -1
49+
this.loading = false
50+
51+
if (this.selectFirst) {
52+
this.down()
53+
}
5154
}
52-
}
53-
})
55+
})
5456
},
5557

56-
fetch () {
58+
fetchDefault(query) {
5759
if (!this.$http) {
5860
return util.warn('You need to install the `vue-resource` plugin', this)
5961
}
@@ -64,13 +66,14 @@ export default {
6466

6567
const src = this.queryParamName
6668
? this.src
67-
: this.src + this.query
69+
: this.src + query
6870

6971
const params = this.queryParamName
70-
? Object.assign({ [this.queryParamName]: this.query }, this.data)
72+
? Object.assign({ [this.queryParamName]: query }, this.data)
7173
: this.data
7274

7375
return this.$http.get(src, { params })
76+
.then(response => response.data)
7477
},
7578

7679
reset () {
@@ -98,17 +101,20 @@ export default {
98101
up () {
99102
if (this.current > 0) {
100103
this.current--
101-
} else if (this.current === -1) {
104+
}
105+
else if (this.current === -1) {
102106
this.current = this.items.length - 1
103-
} else {
107+
}
108+
else {
104109
this.current = -1
105110
}
106111
},
107112

108113
down () {
109114
if (this.current < this.items.length - 1) {
110115
this.current++
111-
} else {
116+
}
117+
else {
112118
this.current = -1
113119
}
114120
},

0 commit comments

Comments
 (0)