页面加载发生在通过xhr以角度返回的数据之前

时间:2016-08-05 11:34:06

标签: javascript jquery angularjs ajax xmlhttprequest

我们在加载从S3提取的文档网址并在仪表板标签页上显示拇指图像方面遇到了挑战。

  /***fetch docs **push success confirm***/
    $scope.getdocuments = function(){
        DDListDataService.getDocumentTypes().then(function(docs){
            $scope.docTypes = docs.data;
        });

        userService.fetchUserDocs($scope.user.customerId,$scope.user.entityType).then(function(userDocs){ 

            //console.log("result--"+userDocs);

            angular.forEach(userDocs, function(object , key){
                console.log(object);
                var xhr = new XMLHttpRequest();
                var blob;
                xhr.open('GET', object.documents[0].documentUrl);
                //var file = common.base64ToBlob(object.d, true);
                xhr.responseType = 'arraybuffer';

                xhr.onload = function(e) {

                    //var file = remoteToBlob(object.documents[0].documentUrl);
                    //var file = common.base64ToBlob(object.documents[0].document, 'image/jpg');
                    /*file.lastModifiedDate = new Date();
                    file.name = "";*/

                    var file = new Blob([this.response], {type: 'image/jpg'});
                    //rest of the code that uses the blob goes here

                    file.lastModifiedDate = new Date();
                    file.name = "";

                    //setTimeout(function(){console.log(file)},1000);

                    var options = {
                            customerId : $scope.user.customerId,
                            customerType : $scope.user.entityType,
                            docTypeName : object.key.documentName,
                            documentName : object.key.documentNameId,
                            documentVersion : object.key.documentVersion,
                            documentUID : object.doccumentUID,
                            //requestedBy : $scope.user.customerType,
                            requestedBy : LocalService.get('userType'),
                            requestedById : $scope.user.customerId

                    }

                    var fileItem = new FileUploader.FileItem($scope.uploader,file,options);
                    fileItem.progress = 100;
                    fileItem.isUploaded = true;
                    fileItem.isSuccess = true;

                    /*file.customerId = $scope.user.customerId;
                    file.customerType =$scope.user.entityType;
                    file.documentName = object.key.documentNameId;
                    file.documentVersion =object.key.documentVersion;
                    file.documentUID = object.doccumentUID;
                    file.requestedBy = $scope.user.customerId;
                    file.requestedById = $scope.user.customerId;*/

                    $scope.uploader.queue.push(fileItem);
                    console.log(fileItem);
                };
                xhr.send();
            });
        });
        //$scope.uploader.formData=[{"user":angular.json($scope.user)}];
        $scope.uploader.queue.length = 0;
        }

执行xhr.send()后,页面加载发生但数据仍然没有准备好,并且它提供了未定义的文件对象,因为平均时间内没有返回数据。在数据返回之前,我们如何处理负载? onload我相信有一个回调应该执行该功能。如果错误,请更正我 怎么办呢? 显示图像的html是

   <div ng-show="uploader.isHTML5" ng-thumb="{ file: item._file, height: 100 ,stream:item._file}"></div>

1 个答案:

答案 0 :(得分:0)

页面加载所需的时间与xhr请求完成所需的时间无关。

因此,创建一个调用另一个函数的window.load函数,并将xhr请求之后所需的内容放在其他函数中,如下所示:

$(window.load(function (){
afterXHRLoad(); //This calls the afterXHRLoad function after the page loads
});

function afterXHRLoad(){
if($.active == 0)
{
//code needed here;
}
else
{
setTimeout(afterXHRLoad, 1000); //recursively call the function after 1 second
}
}