corejavascript / corejavascript/typeahead.js

`this` bound to window? - Expose Input field to the API.

Open
#45 0 comments 0 reactions 0 assignees View on GitHub
enhancement
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 '

'+ (obj.name || obj.street) +' '+ (obj.postcode || "") +'
';
}

return '

'+ (obj.name) +'
';
},
notFound: function(obj){
console.log(this); // Window object
var name = $(":focus").attr("name");
if(name == "form_team_manage_ground"){
return '
Add Ground
'
}

return '

Add Player
';
}
}
})
```

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.