forked from Siyfion/angular-typeahead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-typeahead.js
72 lines (64 loc) · 2.28 KB
/
angular-typeahead.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
angular.module('siyfion.sfTypeahead', [])
.directive('sfTypeahead', function () {
return {
restrict: 'ACE',
scope: {
datasets: '=',
ngModel: '='
},
link: function (scope, element) {
var localChange = false;
element.typeahead(scope.datasets);
function updateScope (object, datum, dataset) {
// for some reason $apply will place [Object] into element, this hacks around it
var preserveVal = element.val();
scope.$apply(function () {
localChange = true;
scope.ngModel = datum;
scope.selectedDataset = dataset;
});
element.val(preserveVal);
}
// Updates the ngModel binding when a value is manually selected from the dropdown.
element.bind('typeahead:selected', function(object, datum, dataset) {
updateScope(object, datum, dataset);
scope.$emit('typeahead:selected');
});
// Updates the ngModel binding when a query is autocompleted.
element.bind('typeahead:autocompleted', function(object, datum, dataset) {
updateScope(object, datum, dataset);
scope.$emit('typeahead:autocompleted');
});
// Updates the ngModel binding when the user manually enters some text
element.bind('input', function () {
scope.$apply(function () {
localChange = true;
var value = element.val();
scope.ngModel = value;
});
});
// Updates typeahead when ngModel changed.
scope.$watch('ngModel', function (newVal) {
var valueKey;
if (localChange) {
localChange = false;
return;
}
if ($.isArray(scope.datasets)) {
for (var i = 0; i < scope.datasets.length; i++) {
if (scope.datasets[i].name == scope.selectedDataset) {
valueKey = scope.datasets[i].valueKey;
break;
}
}
} else {
valueKey = scope.datasets.valueKey;
}
if (newVal && valueKey && newVal.hasOwnProperty(valueKey)) {
newVal = newVal[valueKey];
}
element.typeahead('setQuery', newVal || '');
});
}
};
});