If sforce
is from the Salesforce AJAX Toolkit then you will need to use the async methods.
I strongly suspect the synchronous methods to do sync ajax requests : the browser UI will freeze until every requests complete and the zip result is ready. Even if you use a progress bar, the updates won't show if the UI is frozen.
With async requests, the browser won't freeze anymore : it will be able to display the progress bar updates.
In the code below, fetchDataFromSForces
transforms a sforce
call to a jQuery Deferred (you want jQuery UI so I assumed that you have jQuery on your page). Manually merging async callback can lead to an ugly code and Deferred does that nicely. I didn't copy/paste the block that generate fileIdList
to keep this answer readable, don't forget to put it back.
Without a Salesforce server I can't test this code so there may be some typos, small bugs, etc.
/**
* Fetch data from the server asynchronously and add files in the zip.
* @param {String} id the id of the ContentVersion to fetch.
* @param {JSZip} zip the zip file to fill in.
* @param {SForce} sforce the Sales Force object.
* @return {jQuery.Deferred} the deferred containing the result.
*/
function fetchDataFromSForces(id, zip, sforce) {
var deferred = jQuery.Deferred();
var query = "Select Id,Title,Description,ContentUrl,ContentDocumentId,VersionData,PathOnClient,FileType From ContentVersion WHERE Id = '" + id + "'";
sforce.connection.query(query, {
onSuccess : function (result) {
var currentProgress = $(".your-progressbar").progressbar("option", "value") || 0;
currentProgress++;
// put the result in the zip
var records = result.getArray("records");
var filename = records[0].PathOnClient;
if(filename === '') {
filename = 'ContentPack_'+currentProgress;
}
zip.file(filename, records[0].VersionData,{base64: true});
// update the progress bar
$(".your-progressbar").progressbar("option", "value", currentProgress);
deferred.resolve(zip);
},
onFailure : function (error) {
deferred.reject(error);
}
});
return deferred;
}
// Create your progress bar first.
$(".your-progressbar").progressbar();
document.getElementById("downloadZip").addEventListener("click", function () {
// ...
// Here, the previous code here to init sforce and get fileIdList
// ...
var zip = new JSZip();
var deferreds = [];
for(var key in fileIdList) {
if(fileIdList.hasOwnProperty(key)) {
deferreds.push(fetchDataFromSForces(fileIdList[key], zip, sforce));
}
}
// re-init the progress bar
$(".your-progressbar").progressbar("option", "value", 0);
$(".your-progressbar").progressbar("option", "max", fileIdList.length);
// join all deferreds into one
$.when.apply($, deferreds).done(function () {
var content = zip.generate({type: "blob"});
saveAs(content, "myZip.zip");
var sentTo = document.getElementById("ChooseSentTo").value;
updateDocumentation(checkedRecords,sentTo);
}).fail(function (err) {
document.getElementById("errorMessage").innerHTML = err.message;
document.getElementById("errorMessage").style.color = 'red';
});
});
Here's what I'm doing:
1) In my framework CMakeLists.txt file, I have the following:
IF (APPLE)
SET_TARGET_PROPERTIES( MyFramework PROPERTIES FRAMEWORK true)
SET_TARGET_PROPERTIES( MyFramework PROPERTIES
XCODE_ATTRIBUTE_INSTALL_PATH @executable_path/../Frameworks/ )
ENDIF (APPLE)
The second "set_target_properties" line configures the framework to
always be looked for in the application bundle in the Frameworks
sub-folder.
2) In my top-level CMakeLists.txt file, I add setup a unified binary
output directory:
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Bin)
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Bin )
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Bin )
3) Then, in my applications' CMakeLists.txt file, I have the following:
IF (APPLE)
ADD_CUSTOM_COMMAND(
TARGET MyApp
POST_BUILD
COMMAND ${PYTHON_EXECUTABLE}
ARGS ${CMAKE_HOME_DIRECTORY}/CopyFramework.py
--binary ${PROJECT_BINARY_DIR}/Bin
--framework MyFramework.framework
--app MyApp.app
)
ENDIF (APPLE)
This calls out to my python script, which does the work of assembling
the src and dest paths, and actually copying the Framework.
The final trick is that since this is a Mac only thing, I can rely on
an Xcode environment variable within the Python script:
config= os.environ["CONFIGURATION"]
This allows me to assemble the complete path to the actual binary
locations of the framework and the app.
The one thing I wish was that there was a CMake variable that would
expand to the current Config within the context of the
ADD_CUSTOM_COMMAND... It'd be nice to not have to resort to using the
Xcode environment variable.
Best Answer
You can implement your own download progress system, or a simpler alternative that avoids reinventing the wheel would be to use the excellent ASIHTTPRequest library that provides you with a delegate method for getting the progress of a current download. It's also rather well documented: http://allseeing-i.com/ASIHTTPRequest/