Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a promise in data function. #281

Closed
mdelamere opened this issue Feb 9, 2017 · 1 comment
Closed

Return a promise in data function. #281

mdelamere opened this issue Feb 9, 2017 · 1 comment

Comments

@mdelamere
Copy link

mdelamere commented Feb 9, 2017

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.

        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

@mdelamere
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant