Views.WebForm = Views.Base.extend({
classType: "Views.WebForm",
tagName: "div",
listenEvents: function() {
//See Views.Base listenDefaultEvents()
this.on("beforeinitialize", this.beforeInitialize);
this.on("oninitialize", this.onInitialize);
},
beforeInitialize: function() {
if (this.options.asynchronous) {
this.options.async = this.options.asynchronous;
}
},
onInitialize: function() {
this.questionsList = new Backbone.Collection();
},
selectTemplate: function() {
this._originalCSS = null;
return this.options.descriptor.templates.standard;
},
submitForm: function() {
var that = this;
var oneInputOnError = false;
// console.log(this.questionsList);
this.questionsList.each(function(viewItem) {
var aWebFormQuestion = viewItem.get("view");
aWebFormQuestion.options.childviews.each(function(anInput){
var inputView = anInput.get("view");
if (inputView._isOnError) {
oneInputOnError = true;
}
});
});
if (oneInputOnError) {
alert("Certains champs sont en erreur. Veuillez les renseigner.");
} else {
this.postResponses();
}
},
postResponses: function() {
var that = this;
var url = this.model.get("ReturnPostURL");
var data = this.buildResponseObject();
$.ajax({
url: url ,
data: data,
type: "POST",
dataType:"json",
context: this,
//timeout: 5000,
success: function(data, textStatus, jqXHR) {
$("#webFormContent").hide();
$("#webFormConclusion").show();
},
error: function(jqXHR, textStatus, errorThrown) {
that.displayError(jqXHR, textStatus, errorThrown);
console.log("error :");
console.log("jqXHR :");
console.log(jqXHR);
console.log("textStatus :");
console.log(textStatus);
console.log("errorThrown :");
console.log(errorThrown);
}
});
},
displayError: function(jqXHR, textStatus, errorThrown) {
var str = "An error has occured
";
str += "Status : " + jqXHR.status + "
";
str += "Status text : " + jqXHR.statusText + "
";
$("#webFormErrors").html(str);
$("#webFormContent").hide();
$("#webFormErrors").show();
},
buildResponseObject: function() {
var toReturn = "";
var that = this;
toReturn += '{"FrmID":"' + this.model.get("FrmID") + '",';
toReturn += '"FrpID":"' + this.model.get("FrpID") + '",';
toReturn += '"FrmSeqNum":"' + this.model.get("FrmSeqNum") + '",';
toReturn += '"FrpToken":"' + this.model.get("FrpToken") + '",';
var utk = edApplication().utk;
if (utk) {
toReturn += '"utk":"' + utk + '",';
}
toReturn += '"Responses":[';
var k=0;
var insertComa = false;
this.questionsList.each(function(viewItem) {
if (insertComa) {
toReturn += ',';
}
var aWebFormQuestion = viewItem.get("view");
var oneAnswerFound = false;
aWebFormQuestion.options.childviews.each(function(anInput){
var inputView = anInput.get("view");
if (inputView.classType != "VIEWS.LABEL") {
oneAnswerFound = true;
toReturn += '{"QrsFrpID":"' + that.model.get("FrpID") + '",';
toReturn += '"FqtID":"' + aWebFormQuestion.model.get("FqtID") + '",';
if (aWebFormQuestion.model.get("FqtType") == "REF") {
toReturn += '"QrsRefValID":"' + inputView.model.get("value") + '"';
} else {
if (aWebFormQuestion.model.get("FqtType") == "LST") {
toReturn += '"QrsPosition":"' + inputView.model.get("value") + '"';
} else {
toReturn += '"QrsText":"' + inputView.model.get("value") + '"';
}
}
}
});
k++;
if (oneAnswerFound) {
toReturn += '}';
insertComa = true;
} else {
insertComa = false;
}
});
toReturn += ']';
toReturn += "}";
return toReturn;
}
});
Views.WebFormList = Views.BaseList.extend({
classType: "Views.WebFormList",
listenEvents: function() {
//See Views.Base listenDefaultEvents()
this.on("beforeinitialize", this.beforeInitialize);
},
beforeInitialize: function() {
if (this.options.asynchronous) {
this.options.async = this.options.asynchronous;
}
}
});