2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-25 22:27:39 +00:00

Return instance of Autocomplete for the first matched element if called without arguments.

This commit is contained in:
Tomas Kirda 2013-04-24 14:55:34 -05:00
parent c650a51a15
commit 41543d98a4
2 changed files with 22 additions and 4 deletions

View File

@ -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);
});
});

View File

@ -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') {