尝试在on事件中检索历史消息时,加载时间过长。微调器显示和隐藏得太快。但是该消息尚未加载。
我们如何计算或获取准确的时间来加载历史记录?
$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();**
});
非常感谢您试用PubNub AngularJS API!我会尝试提供一些帮助。在这种情况下,PubNub JS API和PubNub AngularJS API之间存在一些差异。
背景
在后台,PubNub JS APIhistory()
方法立即返回,并在检索到给定的历史“页面”时调用回调。
为了简化此交互,AngularJS API不采用回调-而是$rootScope.$broadcast()
在返回的历史有效负载中调用每个消息。
在此版本的AngularJS API中,当前无法“进入”“ ngHistory”方法以提供回调。但是,有两种解决方案可供您使用:一种一直存在,而第二种是我根据您的反馈刚刚添加的解决方案。
解决方案
1)在此处查看codepen(http://codepen.io/sunnygleason/pen/afqmh)。PubNub AngularJS API中有一个“逃生阴影”,可让您直接为高级用例调用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()
在PubNub AngularJS API的1.2.0-beta.4版本中添加了一个函数。这将使您编写如下代码:
PubNub.ngHistoryQ({channel:theChannel,limit:theLimit}).then(function(payload) {
payload[0].forEach(function(message) {
$scope.messages.push(message);
}
});
您可以使用“ bower install pubnub-angular”安装最新版本的AngularJS SDK。
使用这些解决方案中的任何一个,您都应该能够相应地显示和隐藏微调器。唯一的区别是在#2中,您需要编写如下代码:
var historyPromise = PubNub.ngHistoryQ({channel:theChannel,limit:theLimit});
showSpinner();
historyPromise.then(function(payload) {
// process messages from payload[0] array
hideSpinner();
});
这有帮助吗?让我知道你的想法。再次非常感谢您尝试此操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句