diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index 628b476..e3f4e84 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -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); + }); }); \ No newline at end of file diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 5813b69..527ad9b 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -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(); @@ -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') {