Skip to content

Commit

Permalink
Return instance of Autocomplete for the first matched element if call…
Browse files Browse the repository at this point in the history
…ed without arguments.
  • Loading branch information
Tomas Kirda committed Apr 24, 2013
1 parent c650a51 commit 41543d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 12 additions & 0 deletions spec/autocompleteBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,16 @@ describe('Autocomplete', function () {
expect(input.data('autocomplete')).toBeUndefined();
expect(div.children().length).toBe(0);
});

it('Should return Autocomplete instance if called without arguments', function () {
var input = $(document.createElement('input'));

input.autocomplete({
serviceUrl: '/test-dispose'
});

var instance = input.autocomplete();

expect(instance instanceof $.Autocomplete).toBe(true);
});
});
14 changes: 10 additions & 4 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@

return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
},
dispose: function() {

dispose: function () {
var that = this;
that.el.off('.autocomplete').removeData('autocomplete');
that.disableKillerFn();
Expand All @@ -611,9 +611,15 @@

// Create chainable jQuery plugin:
$.fn.autocomplete = function (options, args) {
var dataKey = 'autocomplete';
// If function invoked without argument return
// instance of the first matched element:
if (arguments.length === 0) {
return this.first().data(dataKey);
}

return this.each(function () {
var dataKey = 'autocomplete',
inputElement = $(this),
var inputElement = $(this),
instance = inputElement.data(dataKey);

if (typeof options === 'string') {
Expand Down

0 comments on commit 41543d9

Please sign in to comment.