Skip to content

Commit

Permalink
add a data variable to pass to the select function
Browse files Browse the repository at this point in the history
  • Loading branch information
Loren Klingman committed Apr 14, 2016
1 parent 9e0cdf3 commit 1730f93
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion angucomplete-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

function callOrAssign(value) {
if (typeof scope.selectedObject === 'function') {
scope.selectedObject(value);
scope.selectedObject(value, scope.selectedObjectData);
}
else {
scope.selectedObject = value;
Expand Down Expand Up @@ -778,6 +778,7 @@
require: '^?form',
scope: {
selectedObject: '=',
selectedObjectData: '=',
disableInput: '=',
initialValue: '=',
localData: '=',
Expand Down
37 changes: 37 additions & 0 deletions test/angucomplete-alt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="countrySelected" selected-object-data="\'test\'" local-data="countries" search-fields="name" title-field="name" minlength="1"/>');
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('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="countrySelected" local-data="countries" search-fields="name" title-field="name" minlength="1" initial-value="initialValue"/>');
Expand Down

0 comments on commit 1730f93

Please sign in to comment.