2

I have this directive :

app.directive('changemonth', function($animator) {
    return {
      link : function($scope, element, attrs) {
        element.bind("click", function() {

          if(element.attr('class').search('disabled') == -1) {

            // récupération du calendrier :
            if(element.attr('class').search('day') != -1)
              var calendar = angular.element(element.parent().parent());
            else                                         
              var calendar = angular.element(element.parent().parent().next().children()[1]);
            var animator = $animator($scope, attrs);

            animator.hide(calendar); 
            setTimeout(function() { 
              $scope.$apply(attrs.changemonth);
              animator.show(calendar); 
            }, 500);
          }
        });
      }
    };
});

With attrs.changemonth, i call a function (wich can change), for exemple this one :

$scope.next = function() {
        var tmpMonth = $scope.monthsLabels.indexOf($scope.monthDisplayed) + 1;
        var tmpYear = $scope.yearDisplayed;
        if(tmpMonth==12) {
            tmpMonth = 0;
            tmpYear = parseInt($scope.yearDisplayed) + 1;
        }
        getCalendar(tmpMonth, tmpYear);
        $scope.monthDisplayed = $scope.monthsLabels[tmpMonth];
        $scope.yearDisplayed = tmpYear.toString();
    };

And so this function call another getCalendar() you can see here :

function getCalendar(month, year) {
        $http({
            method : "GET", 
            url : 'http://my_url/getCalendar', 
            params : { 
                group_id : $scope.group_id,
                month : month,
                year : year
            }
        })
        .success(function(data) {
            $scope.calendar = data;
        });
    }

getCalendar() use $http to get the calendar from database.

My problem is that i would like to wait for the response of $http before using animator in directive, like this, my calendar will only be displayed if the content is loaded.

I heard of $q and promises. But I don't see how to use it in this very particular context.

It would be awesome if somebody here have an idea.

2 Answers 2

4

Try broadcasting from your success callback like this.

.success(function(data) {
    $scope.calendar = data;
    $rootScope.$broadcast('event:calendar-received');
});

Then in your directive you can wait to receive that signal like this.

$scope.$on('event:calendar-received', function() {
    ... do your stuff with animator...
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot, it works parfectly and i needed just a very little changing.
I just have one problem now. As I have multiple calls of $scope.$on('event:calendar-received', function() {}, I can't make the differences between my differents calls. So the event launch sometimes with the parameters of the last $broadcast not the earliest. How could i make all $broadcast indepedent from each other ? thanks again for help.
OK, i just added some unique token to the $broadcast string and it works perfectly.
0

$http(...) evaluates to a promise. That means that given

var x = $http(...)

You can do

x.then(function success(){...}, function failure(){...});

and the success or failure function will only be called when the promise is resolved. See the promise API.

Your functions can return this x and their calling functions can interact with it as a promise.

1 Comment

Thanks for the help, i tried but it seems that something goes wrong. Technicaly your tip works but it doesn't works well with my other stuffs. Thanks again for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.