corejavascript / corejavascript/typeahead.js
`this` bound to window? - Expose Input field to the API.
- Dominant language
- JavaScript
- Stars
- 968
- Forks
- 231
- PR merge metrics
- No merged PRs in 30d
Description
I am really struggling with trying to grab a reference to the input field. Internally there seems to be a `this.$input` that should relate the the current input field. However, this isn't accessible through the API (to my knowledge) and all references to `this` return the `window` object.
The problem I am finding is due to the fact that I'm binding to multiple inputs, this is clearly not the default behaviour that the twitter devs intended, but I believe it makes sense.
```
var data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("name"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.objectId; },
remote: {
url: '/team/management/suggest',
prepare: function(query, request){
console.log(this); // Bloodhound object
var name = $(":focus").attr("name");
request.url = request.url + '?'+ name +'=' + query;
return request;
}
}
});
$('[role="typeahead"]').typeahead({
minLength: 2,
highlight: true,
classNames: {
wrapper: "suggestion-wrapper"
}
}, {
name: 'players',
display: 'name',
source: data,
limit: 100,
templates: {
suggestion: function(obj){
console.log($(this)); // Window object
var name = $(":focus").attr("name");
if(name == "form_team_manage_ground"){
return '
}
return '
},
notFound: function(obj){
console.log(this); // Window object
var name = $(":focus").attr("name");
if(name == "form_team_manage_ground"){
return '
}
return '
}
}
})
```
This was working well, until the latest release of Safari, it seems that `:focus` doesn't populate until after the suggestion engine kicks in, this means that I get `undefined`.
I would like for the input field to be exposed so that we don't need to rely on `:focus`.
Can somebody point me in the right direction as this library's source seems highly complex?
Cheers.
Contributor guide
Assessment
This issue has not been assessed yet.