Hi Guys,
I have a very strange problem when trying to call an Angular function (bound to $scope) after the stop callback for the JQuery File Uploader. I'm using the Uploader to upload some image files to my server. The code within the Angular controller looks like this:
$scope.ImageGallery = [];
$scope.InitialiseUploader = function () {
var fileUploaderURL = "../app_plugins/imagegallery/backoffice/uploader/server/uploaderhandler.ashx";
// Change this to the location of your server-side upload handler:
$('#fileupload').fileupload({
url: fileUploaderURL,
dropZone: $("#drop-zone"),
maxChunkSize: 1000000, // 1MB
limitConcurrentUploads: 3,
formData: {
GalleryID: $scope.GalleryID
},
dataType: 'json',
drop: function (e, data) {
$('#progress .progress-bar').css(
'width',
0 + '%'
);
},
done: function (e, data) {
// Current image uploaded
},
stop: function (e) {
// All done
$scope.RefreshGallery();
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
},
fail: function (e, data) {
alert(data.errorThrown);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
}
$scope.RefreshGallery = function() {
alert("refreshing gallery");
$http.get("/umbraco/ImageGallery/ImageGalleryApi/GetImages/" + $scope.GalleryID).
then(function (response) {
// Success
if (response.data) {
$scope.ImageGallery = angular.fromJson(response.data);
}
}, function (response) {
// Failure, umbraco will also catch this
alert("failed");
});
}
The problem is that although the stop function successfully calls the RefreshGallery function (I can see this becauase the test alert box displays "refreshing gallery"), the $http.get web call does not take place until I click in the browser window after which it executes immediately. I have tried debugging through the code and everything is called properly. During debug, and executes correctly. However as soon as I detach the the debugger and remove the alert box, attached the $http.get doesn't return until I click on the page. I have debugged the web service on the server end and can confirm that no attempt is made to even call the service until I click in the browser window.
It looks like some sort of odd threading issue and is perhaps something to do with the way in which I have written the code (I'm fairly new to Angular). I've been playing around with this for several days now and have had no luck in getting it to work automatically once the uploads are complete without clicking on the window. The way I intend it to work is to make the web call to refresh the gallery automatically as soon as it is called by the stop event on the file uploader.
If I manually bind the RefreshGallery function to a button it works every time, it just doesn't work when called from the stop function.
Does anything have any idea what I'm doing wrong? I'm obviously missing something here so an explanation would be very useful.
Many thanks,
Damien