How to implement callback in ngHistory in Pubnub?

When trying to get a history message in the on event , the load time is too long. The curler shows and hides too quickly. But the message has not yet been downloaded.

How can we calculate or get the exact time to load the story?

 $scope.limit = 100
 PubNub.ngHistory( {
                        channel : $scope.channel,
                        limit   : $scope.limit
                        });

   $rootScope.$on(PubNub.ngMsgEv($scope.channel), function(ngEvent, payload) {

                **ActivityIndicator.showSpinner();**

                    $scope.$apply(function(){
                    $scope.messages.push(payload.message);
                });

                $(".messages-wrap").scrollTop($(".messages-wrap")[0].scrollHeight);
                **ActivityIndicator.hideSpinner();**

            }); 
+2
source share
2 answers

Thank you so much for trying out the PubNub AngularJS API! I will try to provide some help. There is a little difference between the PubNub JS API and the AngularJS PubNub API in this case.

Background

Behind the scenes, the PubNub JS API method history()returns instantly, and invokes a callback when this story page is retrieved.

API AngularJS, , - $rootScope.$broadcast() .

API AngularJS " ", "ngHistory" . 2 : , , .

1) . codepen (http://codepen.io/sunnygleason/pen/afqmh). API PubNub AngularJS "escape-hatch", JS API , jsapi. PubNub.jsapi.history({channel:theChannel,limit:theLimit,callback:theCallback}). , , , $rootScope, $rootScope.$apply() $scope.$apply(), , , $scope , .

2) . codepen (http://codepen.io/sunnygleason/pen/JIsek). , ngHistoryQ() 1.2.0-beta.4 API PubNub AngularJS. , :

PubNub.ngHistoryQ({channel:theChannel,limit:theLimit}).then(function(payload) {
  payload[0].forEach(function(message) {
    $scope.messages.push(message);
  }
});

SDK AngularJS, "bower install pubnub- angular".

. # 2, :

var historyPromise = PubNub.ngHistoryQ({channel:theChannel,limit:theLimit});
showSpinner();
historyPromise.then(function(payload) {
  // process messages from payload[0] array
  hideSpinner();
});

? , . .

+4

?

0

All Articles