Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'FH-2290_Fix-Download-Submission-To-Use-Events'
Browse files Browse the repository at this point in the history
  • Loading branch information
nialldonnellyfh committed Oct 8, 2015
2 parents 0674637 + 1f9b861 commit 9d4de00
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 57 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Changelog - FeedHenry Javascript SDK

## 2.10.0 - 2015-10-07 - Niall Donnelly

* FH-2290 - Added Event Driven Approach To $fh.forms.downloadSubmission API.

## 2.9.0 - 2015-10-06 - Niall Donnelly

* FH-2330 - Added A Global Event Listener For Appforms Models
* FH-2340 - Populating the _id parameter for uploaded submissions
* FH-2340 - Populating the _id parameter for uploaded submissions.

## 2.8.0 - 2015-10-02 - Niall Donnelly/Wei Li/Shannon Poole

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fh-js-sdk",
"version": "2.9.0",
"version": "2.10.0",
"homepage": "https://github.com/feedhenry/fh-js-sdk",
"authors": [
"[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fh-js-sdk",
"version": "2.9.0",
"version": "2.10.0",
"description": "feedhenry js sdk",
"main": "dist/feedhenry.js",
"browser": {
Expand Down
111 changes: 59 additions & 52 deletions src/appforms/src/core/050-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ appForm.api = function (module) {
module.emit = function () {
var args = Array.prototype.slice.call(arguments, 0);
var eventName = args.shift();
var funcDetails = module._events[eventName];
if (funcDetails && funcDetails.length > 0) {
for (var i = 0; i < funcDetails.length; i++) {
var functionToCall = funcDetails[i].func;
var funcDetailsArray = module._events[eventName];
if (funcDetailsArray && funcDetailsArray.length > 0) {
for (var i = 0; i < funcDetailsArray.length; i++) {
var functionDetails = funcDetailsArray[i];
var functionToCall = funcDetailsArray[i].func;
//If the function was not already called, or is not only set to call once, the call the function,
//Otherwise, don't call it.
if(!funcDetails.called || !funcDetails.callOnce){
funcDetails.called = true;
if(!functionDetails.called || !functionDetails.callOnce){
functionDetails.called = true;
functionToCall.apply(this, args);
}
}
Expand Down Expand Up @@ -276,14 +277,19 @@ appForm.api = function (module) {
* @param {function} cb (err, downloadTask)
* */
function downloadSubmission(params, cb) {
$fh.forms.log.d("downloadSubmission called", params);
params = params ? params : {};
//cb = cb ? cb : defaultFunction;
var submissionToDownload = null;
var waitCallbackPassed = typeof(cb) === "function";
cb = typeof(cb) === "function" ? cb : function(){};

if(typeof(cb) !== 'function'){
return null;
//There should be a submission id to download.
if(!params.submissionId){
$fh.forms.log.e("No submissionId passed to download a submission");
return cb("No submissionId passed to download a submission");
}

var submissionToDownload = null;

function finishSubmissionDownload(err) {
err = typeof(err) === "string" && err.length === 24 ? null : err;
$fh.forms.log.d("finishSubmissionDownload ", err, submissionToDownload);
Expand All @@ -305,64 +311,65 @@ appForm.api = function (module) {
}
}

$fh.forms.log.d("downloadSubmission called", params);
$fh.forms.log.d("downloadSubmission SubmissionId exists" + params.submissionId);
var submissionAlreadySaved = appForm.models.submissions.findMetaByRemoteId(params.submissionId);

if (params.submissionId) {
$fh.forms.log.d("downloadSubmission SubmissionId exists" + params.submissionId);
var submissionAlreadySaved = appForm.models.submissions.findMetaByRemoteId(params.submissionId);
if (submissionAlreadySaved === null) {

if (submissionAlreadySaved === null) {
$fh.forms.log.d("downloadSubmission submission does not exist, downloading", params);
submissionToDownload = new appForm.models.submission.newInstance(null, {
submissionId: params.submissionId
});

$fh.forms.log.d("downloadSubmission submission does not exist, downloading", params);
submissionToDownload = new appForm.models.submission.newInstance(null, {
submissionId: params.submissionId
});
submissionToDownload.on('error', finishSubmissionDownload);

submissionToDownload.on('downloaded', finishSubmissionDownload);

submissionToDownload.on('error', finishSubmissionDownload);
if (typeof(params.updateFunction) === 'function') {
submissionToDownload.on('progress', params.updateFunction);
}

submissionToDownload.on('downloaded', finishSubmissionDownload);
//If there is no callback function, then just trigger the download.
//Users can register global listeners for submission downloads events now.
if(typeof(cb) === "function"){
if(waitOnSubmission[params.submissionId]){
waitOnSubmission[params.submissionId].push(cb);
} else {
waitOnSubmission[params.submissionId] = [];
waitOnSubmission[params.submissionId].push(cb);
}
}

if (typeof(params.updateFunction) === 'function') {
submissionToDownload.on('progress', params.updateFunction);
submissionToDownload.download(function(err) {
if (err) {
$fh.forms.log.e("Error queueing submission for download " + err);
return cb(err);
}
});
} else {
$fh.forms.log.d("downloadSubmission submission exists", params);


//Submission was created, but not finished downloading
if (submissionAlreadySaved.status !== "downloaded" && submissionAlreadySaved.status !== "submitted") {
if(typeof(cb) === "function"){
if(waitOnSubmission[params.submissionId]){
waitOnSubmission[params.submissionId].push(cb);
waitOnSubmission[params.submissionId].push(cb);
} else {
waitOnSubmission[params.submissionId] = [];
waitOnSubmission[params.submissionId].push(cb);
}
}

submissionToDownload.download(function(err) {
if (err) {
$fh.forms.log.e("Error queueing submission for download " + err);
return cb(err);
waitOnSubmission[params.submissionId] = [];
waitOnSubmission[params.submissionId].push(cb);
}
});
}
} else {
$fh.forms.log.d("downloadSubmission submission exists", params);

//Submission was created, but not finished downloading
if (submissionAlreadySaved.status !== "downloaded" && submissionAlreadySaved.status !== "submitted") {
if(typeof(cb) === "function"){
if(waitOnSubmission[params.submissionId]){
waitOnSubmission[params.submissionId].push(cb);
} else {
waitOnSubmission[params.submissionId] = [];
waitOnSubmission[params.submissionId].push(cb);
}
appForm.models.submissions.getSubmissionByMeta(submissionAlreadySaved, function(err, submission){
if(err){
return cb(err);
}
} else {
appForm.models.submissions.getSubmissionByMeta(submissionAlreadySaved, cb);
}

//If the submission has already been downloaded - emit the downloaded event again
submission.emit('downloaded', submission.getRemoteSubmissionId());
return cb(undefined, submission);
});
}
} else {
$fh.forms.log.e("No submissionId passed to download a submission");
return cb("No submissionId passed to download a submission");
}
}
return module;
Expand Down
28 changes: 27 additions & 1 deletion src/appforms/tests/tests/core/050-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,35 @@ describe("$fh.forms API", function() {
assert.ok(submission);
console.log("Submission Data: ", submission);
assert.ok(submission.getRemoteSubmissionId().length > 0);
done();
submission.clearLocal(function(err){
assert.ok(!err, "Expected No Error When Clearing Submission");
done(err);
});
});
});
it("$fh.forms.downloadSubmission No Callback. Global Event Listener Instead", function(done){
this.timeout(3000);
var submissionId = "submissionData";
var downloadSubmission = appForm.api.downloadSubmission;

appForm.api.once('submission:error', function(err){
assert.ok(!err, "Expected No Error " + err);
done(err);
});

appForm.api.once('submission:downloaded', function(remoteSubmissionId){
assert.equal("submissionData", remoteSubmissionId);
assert.equal("submissionData", this.getRemoteSubmissionId());
//Clearing Out The Submission.
this.clearLocal(function(err){
assert.ok(!err, "Expected No Error When Clearing Submission");
done(err);
});
});

//In this case, the submission is queued for download, but does not wait for a download to complete.
downloadSubmission({submissionId: submissionId});
});
it("$fh.forms.downloadSubmission with files", function(done){
this.timeout(10000);
var submissionId = "submissionFile";
Expand Down

0 comments on commit 9d4de00

Please sign in to comment.