Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a custom fetch function to handle things like auth, etc... #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/

.idea
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ export default {
// Override the default value (`q`) of query parameter name
// Use a falsy value for RESTful query
// (optional)
queryParamName: 'search'
queryParamName: 'search',

// Override the 'src' prop and rather use your own fetching function
// Useful for authentication, custom headers, etc...
// (optional)
fetchFunction: function(input) {
// Fetch the result here and return an array of items
}
}
},

Expand Down
15 changes: 9 additions & 6 deletions dist/vue-typeahead.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ exports.default = {

this.loading = true;

this.fetch().then(function (response) {
var suggestionPromise = this.fetchFunction ? this.fetchFunction(this.query) : this.fetchDefault(this.query);

suggestionPromise.then(function (data) {
if (_this.query) {
var data = response.data;
data = _this.prepareResponseData ? _this.prepareResponseData(data) : data;
_this.items = _this.limit ? data.slice(0, _this.limit) : data;
_this.current = -1;
Expand All @@ -69,7 +70,7 @@ exports.default = {
}
});
},
fetch: function fetch() {
fetchDefault: function fetchDefault(query) {
if (!this.$http) {
return _vue.util.warn('You need to install the `vue-resource` plugin', this);
}
Expand All @@ -78,11 +79,13 @@ exports.default = {
return _vue.util.warn('You need to set the `src` property', this);
}

var src = this.queryParamName ? this.src : this.src + this.query;
var src = this.queryParamName ? this.src : this.src + query;

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

return this.$http.get(src, { params: params });
return this.$http.get(src, { params: params }).then(function (response) {
return response.data;
});
},
reset: function reset() {
this.items = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"vue-resource": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-plugin-transform-runtime": "^6.0.0",
Expand Down
42 changes: 24 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ export default {

this.loading = true

this.fetch().then((response) => {
if (this.query) {
let data = response.data
data = this.prepareResponseData ? this.prepareResponseData(data) : data
this.items = this.limit ? data.slice(0, this.limit) : data
this.current = -1
this.loading = false

if (this.selectFirst) {
this.down()
const suggestionPromise = this.fetchFunction ? this.fetchFunction(this.query) : this.fetchDefault(this.query);

suggestionPromise
.then((data) => {
if (this.query) {
data = this.prepareResponseData ? this.prepareResponseData(data) : data
this.items = this.limit ? data.slice(0, this.limit) : data
this.current = -1
this.loading = false

if (this.selectFirst) {
this.down()
}
}
}
})
})
},

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

const src = this.queryParamName
? this.src
: this.src + this.query
: this.src + query

const params = this.queryParamName
? Object.assign({ [this.queryParamName]: this.query }, this.data)
? Object.assign({ [this.queryParamName]: query }, this.data)
: this.data

return this.$http.get(src, { params })
.then(response => response.data)
},

reset () {
Expand Down Expand Up @@ -98,17 +101,20 @@ export default {
up () {
if (this.current > 0) {
this.current--
} else if (this.current === -1) {
}
else if (this.current === -1) {
this.current = this.items.length - 1
} else {
}
else {
this.current = -1
}
},

down () {
if (this.current < this.items.length - 1) {
this.current++
} else {
}
else {
this.current = -1
}
},
Expand Down