Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

feat(typeahead): adding/removing class when source is promise #321

Closed
skytracer opened this issue Apr 12, 2013 · 4 comments
Closed

feat(typeahead): adding/removing class when source is promise #321

skytracer opened this issue Apr 12, 2013 · 4 comments

Comments

@skytracer
Copy link

I think it would be nice to set a class to mark the beginning and the end of the async source pull
I am using this to show a spinner at the right end of the input.
This is my code

             var getMatchesAsync = function(inputValue) {

                    var locals = {$viewValue: inputValue};
                    element.addClass('bs-searching');

                    $q.when(parserResult.source(scope, locals)).then(function(matches) {

                        //it might happen that several async queries were in progress if a user were typing fast
                        //but we are interested only in responses that correspond to the current view value
                        if (inputValue === modelCtrl.$viewValue) {
                            if (matches.length > 0) {

                                scope.activeIdx = 0;
                                scope.matches.length = 0;

                                //transform labels
                                for (var i = 0; i < matches.length; i++) {
                                    locals[parserResult.itemName] = matches[i];
                                    scope.matches.push({
                                        label: parserResult.viewMapper(scope, locals),
                                        model: matches[i]
                                    });
                                }

                                scope.query = inputValue;
                                element.removeClass('bs-searching');

                            } else {
                                resetMatches();
                            }
                        }
                    }, resetMatches);
                };
@pkozlowski-opensource
Copy link
Member

@skytracer thnx for your input and inspiration but I don't think we should be adding classes like this. Doing so would mean that we are doing DOM manipulation on every keystroke and also we would be hard-coding a CSS class.

But the use-case you've got is very valid so I would propose to solve it slightly differently. The typeahead directive could expose the loading binding expression (would have to be a settable property). Then you could use it with ng-class to style an input, ex:

<input ng-model="mymodel" typeahead="..." typeahead-loading="isLoading" ng-class="{myLoadingStyle: isLoading}">

WDYT?

@pkozlowski-opensource
Copy link
Member

BTW: it is somehow linked to #192 although here we should really focus on exposing a binding variable only. This would actually allow people to implement #192 "by hand". I still think we should implement #192 to have a default loading behavior.

@pkozlowski-opensource
Copy link
Member

@skytracer I've implemented an alternative, model-oriented approach in b58c9c8, as described above. Let me know what do you think.

@skytracer
Copy link
Author

@pkozlowski-opensource I think your approach opens more use-cases and I am happy with it
Thanks

codedogfish pushed a commit to codedogfish/angular-ui-bootstrap that referenced this issue Sep 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants