Update 2
Ah, your service returns items that actually don't match the query parm
. Your typeahead query is 'parm'
, with which none of the returned results match. You can override the matcher
function used by the typeahead plugin, see the docs. Simply implement it as return true
to match all results returned by your service.
Update 1
This is an updated version, that maps the name to an id, which can be used later. A jsfiddle is available.
var nameIdMap = {};
$('#lookup').typeahead({
source: function (query, process) {
return $.ajax({
dataType: "json",
url: lookupUrl,
data: getAjaxRequestData(),
type: 'POST',
success: function (json) {
process(getOptionsFromJson(json));
}
});
},
minLength: 1,
updater: function (item) {
console.log('selected id'+nameIdMap[item]);
return item;
}
});
function getOptionsFromJson(json) {
$.each(json, function (i, v) {
nameIdMap[v.name] = v.id;
});
return $.map(json, function (n, i) {
return n.name;
});
}
Original answer
You need to make the call async and call the process
callback from within the success callback like this:
$('#companyNameInput').typeahead({
source: function (query, process) {
$.ajax({
url: ROOT + 'Record/checkCompanyName',
// async: false, // better go async
data: 'q=' + query,
type: 'POST',
cache: false,
success: function (data) {
var companiesFinal = ... // snip
process(companiesFinal);
}
})
}
});
The return ['test1', 'test2'];
works, because the source function is then basically set to:
// do ajax stuff, but do nothing with the result
// return the typeahead array, which the typeahead will accept as the result:
return ['test1', 'test2'];
Notes
There is a one-liner to fill companiesData
:
var companiesFinal = return $.map(data, function (n, i) { n.name; });
And you probably want to declare your variables using var
; otherwise they'll have global scope, which will bite you.
Best Answer
Well, it seems that if you are referencing a Role that doesn't exist then you get the trusted domain error.
The error went away and the code functioned just fine when I changed from ...
to the actual group name ...