You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to return a promise in a data function? I have tried the following and it does not seem to work. The issue is that I am trying to make an ajax call within the data-function, which expects a result/data array. Of course I cannot do this when making an asynchronous ajax call.
var ms = $('#mycombo').magicSuggest({minChars: 2, data : function(q) {
return someAPI.findSuggestions(q, currentLang).then(function(response) {
if(!_.isEmpty(response.data.suggestions)) {
_.each(response.data.suggestions, function(suggestion) {
if (suggestion.id && suggestion.label) {
data.push({ id: suggestion.id, name: suggestion.label });
}
});
}
});
return data;
}});
Thanks in advance for your help.
Michael
The text was updated successfully, but these errors were encountered:
Okay, I have found out the solution myself now. It is quite simple when you know how. Basically you need to forget the data-property during initialization of magicsuggest. Instead I now used the keyup event as described in the documentation:
var ms = $('#mycombo').magicSuggest({minChars: 2});
$(ms).on('keyup', function(e, m, v) {
// ... get data via ajax and call the following in the response callback ...
ms.setData(data);
}
Obviously this will cause a request to be executed on every keyup event, which is most likely not what we want. So a bit of extra work is necessary but at least this is the solution to the initial problem.
Hi,
Is it possible to return a promise in a data function? I have tried the following and it does not seem to work. The issue is that I am trying to make an ajax call within the data-function, which expects a result/data array. Of course I cannot do this when making an asynchronous ajax call.
Thanks in advance for your help.
Michael
The text was updated successfully, but these errors were encountered: