vendredi 27 février 2015

Using $timeout, scope is updated running localhost, but not remotely

I want to walk through an array slowly, doing something asynch for each element and present status as I go. Running on a local node server, I can see the status appear as expected, but when the same code is served from parseapps.com, no status appears. Any ideas why?


In the controller ...



function doSomethingSlow(inputArray, cursor) {
cursor = (cursor === undefined)? 0 : cursor;
var inputLength = inputArray.length;
var input = inputArray[cursor];
doSomethingAsynch(input).then(function(result) {
$timeout(function(){
$scope.status = result;
if (cursor < inputLength-1 && $scope.continue) {
doSomethingSlow(inputArray, cursor+1);
}
}, 1000);
}, function(error) {
alert("error");
});
}


In the mark-up ...



<div class="row" ng-if="status">
<div class="col-sm-12">
<div class="alert alert-success">{{status}}</div>
</div>
</div>


I figure the problem has something to do with when and how scope is applied in the timeout, but I don't understand enough to fix it. Besides, why would the server matter? I've tested in Chrome and Safari, on local and remote server. The function works in all cases, but in the remote case, the UI doesn't get updated.


I'd be happy if someone can suggest a better design. The cursor and the $timeout, etc seem pretty clunky and verbose for what sounds like a simple thing.


Aucun commentaire:

Enregistrer un commentaire