diff --git a/README.md b/README.md index f1613ea6..5beace84 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ It expects the returned results from remote API to have a root object. In the ab | maxlength | Maxlength attribute for the search field. [example](https://ghiden.github.io/angucomplete-alt/#example1) | No | attribute | 25 | | pause | The time to wait (in milliseconds) before searching when the user enters new characters. [example](https://ghiden.github.io/angucomplete-alt/#example1) | No | @ | 400 | | selected-object | Either an object in your scope or callback function. If you set an object, it will be passed to the directive with '=' sign but it is actually one-way-bound data. So, setting it from your scope has no effect on input string. If you set a callback, it gets called when selection is made. To get attributes of the input from which the assignment was made, use this.$parent.$index within your function. [example](https://ghiden.github.io/angucomplete-alt/#example1) | Yes | = | selectedObject or objectSelectedCallback | +| selected-object-data | A second parameter which will be passed to selected-object. Only works when using selected-object. | No | = | row | | remote-url | The remote URL to hit to query for results in JSON. angucomplete will automatically append the search string on the end of this, so it must be a GET request. [example](https://ghiden.github.io/angucomplete-alt/#example5) | No | @ | http://myserver.com/api/users/find?searchstr= | | remote-url-data-field | The name of the field in the JSON object returned back that holds the Array of objects to be used for the autocomplete list. [example](https://ghiden.github.io/angucomplete-alt/#example5) | No | @ | results | | title-field | The name of the field in the JSON objects returned back that should be used for displaying the title in the autocomplete list. Note, if you want to combine fields together, you can comma separate them here (e.g. for a first and last name combined). If you want to access nested field, use dot to connect attributes (e.g. name.first). [example](https://ghiden.github.io/angucomplete-alt/#example1) | Yes | @ | firstName,lastName | diff --git a/angucomplete-alt.js b/angucomplete-alt.js index cb5b22c4..dd725cfb 100644 --- a/angucomplete-alt.js +++ b/angucomplete-alt.js @@ -163,7 +163,7 @@ function callOrAssign(value) { if (typeof scope.selectedObject === 'function') { - scope.selectedObject(value); + scope.selectedObject(value, scope.selectedObjectData); } else { scope.selectedObject = value; @@ -778,6 +778,7 @@ require: '^?form', scope: { selectedObject: '=', + selectedObjectData: '=', disableInput: '=', initialValue: '=', localData: '=', diff --git a/test/angucomplete-alt.spec.js b/test/angucomplete-alt.spec.js index 1cea1dd3..4858e258 100644 --- a/test/angucomplete-alt.spec.js +++ b/test/angucomplete-alt.spec.js @@ -994,6 +994,43 @@ describe('angucomplete-alt', function() { }); }); + describe('selectedObject callback with extra paramater', function() { + it('should call selectedObject callback if given', function() { + var element = angular.element('
'); + var selected = false; + $scope.countrySelected = function(value, row) { + selected = row; + }; + $scope.countries = [ + {name: 'Afghanistan', code: 'AF'}, + {name: 'Aland Islands', code: 'AX'}, + {name: 'Albania', code: 'AL'} + ]; + $compile(element)($scope); + $scope.$digest(); + + expect(selected).toBe(false); + var inputField = element.find('#ex1_value'); + var eKeyup = $.Event('keyup'); + eKeyup.which = 97; // letter: a + + inputField.val('a'); + inputField.trigger('input'); + inputField.trigger(eKeyup); + $timeout.flush(); + expect(element.find('#ex1_dropdown').length).toBe(1); + + var eKeydown = $.Event('keydown'); + eKeydown.which = KEY_DW; + inputField.trigger(eKeydown); + expect(element.isolateScope().currentIndex).toBe(0); + + eKeydown.which = KEY_EN; + inputField.trigger(eKeydown); + expect(selected).toBe('test'); + }); + }); + describe('initial value', function() { it('should set initial value from string', function() { var element = angular.element('');