Skip to content

Commit

Permalink
Merge pull request #9 from therebelrobot/feature/5-callback-on-update
Browse files Browse the repository at this point in the history
Callback on update
  • Loading branch information
marcojetson committed Sep 1, 2015
2 parents 1f09bc0 + 2dd8ff0 commit f878518
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ t.update([
])
```

### Callback

If you want to run code after autocomplete updates the input (e.g. to update a model), simply add a `callback` function into the `opts` parameter:

```javascript
var opts = {
callback:function(newValue){
console.log(newValue);
// Do code here
}
};

var t = new TypeAhead(document.getElementById('my-control'), [
'Asia', 'Africa', 'Europe', 'North America', 'South America', 'Oceania'
], opts);
```

## Contributing

Found an issue? Have a feature request? Open a [Github Issue]() and/or [fork this repo]().
Expand Down
3 changes: 3 additions & 0 deletions src/type-ahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var TypeAhead = function (element, candidates, opts) {

typeAhead.limit = opts.hasOwnProperty('limit') ? opts.limit : 5;

typeAhead.callback = opts.hasOwnProperty('callback') ? opts.callback : function(){};

typeAhead.query = '';

typeAhead.selected = null;
Expand Down Expand Up @@ -90,6 +92,7 @@ TypeAhead.prototype.handleKeyDown = function (keyCode) {
if (keyCode === 13 && !this.list.isEmpty()) {
this.value(this.list.items[this.list.active]);
this.list.hide();
this.callback(this.list.items[this.list.active]);
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions type-ahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var TypeAhead = function (element, candidates, opts) {

typeAhead.limit = opts.hasOwnProperty('limit') ? opts.limit : 5;

typeAhead.callback = opts.hasOwnProperty('callback') ? opts.callback : function(){};

typeAhead.query = '';

typeAhead.selected = null;
Expand Down Expand Up @@ -91,6 +93,7 @@ TypeAhead.prototype.handleKeyDown = function (keyCode) {
if (keyCode === 13 && !this.list.isEmpty()) {
this.value(this.list.items[this.list.active]);
this.list.hide();
this.callback(this.list.items[this.list.active]);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion type-ahead.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f878518

Please sign in to comment.