I am working on a web page where I am creating a JS object using {} and []. This object is passed into a handler. I wanted to see if there is a better way to create the object in JavaScript
My logic
Is to build the object is JS and then pass it into the Handler to be Deserialize and then saved into the db.
Can anyone please review this code and point out the mistakes/how to make the implementation better?
const MeetingPollingQuestionTypeId = {
LongAnswerText: 1,
MultipleChoice: 2,
MultipleChoiceGrid:3
};
const MeetingPollingPartsTypeId = {
Question: 1,
Image: 2,
Answer: 3
};
const MeetingPollingPartsValuesTypeId = {
Label: 1,
Img: 2,
Radio:3
}
function saveMultipleChoice() {
console.log('%c saveMultipleChoice ', 'background: #222; color: #bada55');
let sequenceorder = fnSequenceOrder();
MeetingPollingQuestion = {};
MeetingPollingQuestion.MeetingPollingId = $("#hfMeetingPollingId").val();
MeetingPollingQuestion.MeetingPollingQuestionType = "MultipleChoice";
MeetingPollingQuestion.SequenceOrder = sequenceorder;
MeetingPollingQuestion.MeetingPollingParts = [];
MeetingPollingParts = {};
MeetingPollingParts.MeetingPollingPartsTypeId = MeetingPollingPartsTypeId.Question;
MeetingPollingParts.MeetingPollingPartsValues = [];
MeetingPollingPartsValues = {};
MeetingPollingPartsValues.MeetingPollingPartsValuesTypeId = MeetingPollingPartsValuesTypeId.label,
MeetingPollingPartsValues.QuestionValue = $("#editor").data("kendoEditor").value();
MeetingPollingParts.MeetingPollingPartsValues.push(MeetingPollingPartsValues);
MeetingPollingQuestion.MeetingPollingParts.push(MeetingPollingParts);
MeetingPollingParts = {};
MeetingPollingParts.MeetingPollingPartsTypeId = MeetingPollingPartsTypeId.Image;
MeetingPollingParts.MeetingPollingPartsValues = [];
MeetingPollingPartsValues = {};
MeetingPollingPartsValues.Type = "FileManagerId";
MeetingPollingPartsValues.FileManagerId = $("#hfFileManagerId").val();
MeetingPollingParts.MeetingPollingPartsValues.push(MeetingPollingPartsValues);
MeetingPollingQuestion.MeetingPollingParts.push(MeetingPollingParts);
MeetingPollingParts = {};
MeetingPollingParts.MeetingPollingPartsTypeId = MeetingPollingPartsTypeId.Answer;
MeetingPollingParts.MeetingPollingPartsValues = [];
var items = $("#selectanswer").data("kendoMultiSelect");
var selectedDataItems = items.dataItems();
$(selectedDataItems).each(function () {
MeetingPollingPartsValues = {};
MeetingPollingPartsValues.MeetingPollingPartsValuesTypeId = MeetingPollingPartsValuesTypeId.Radio
MeetingPollingPartsValues.QuestionValue = this.text;
MeetingPollingParts.MeetingPollingPartsValues.push(MeetingPollingPartsValues);
});
MeetingPollingParts.MeetingPollingPartsValues.push(MeetingPollingPartsValues);
MeetingPollingQuestion.MeetingPollingParts.push(MeetingPollingParts);
console.log(MeetingPollingQuestion);
Metronic.blockUI({ boxed: true, message: "Saving Question.." });
$.ajax({
type: 'POST',
url: 'ManagePolling.ashx',
data: { "PollingQuestion": JSON.stringify(MeetingPollingQuestion), "Action": "SaveQuestion" },
datatype: "JSON",
success: function (data) {
if (data.resultStatus.ResultCode == "1") {
toastr.success("Saved successfully", "Success");
console.log('%c MeetingPollingQuestion Success! ', 'background: #222; color: #bada55');
htmlBuilderMultipleChoice(MeetingPollingQuestion);
}
if (data.resultStatus.ResultCode == "2")
toastr.warning(data.resultStatus.Message, "Warning");
if (data.resultStatus.ResultCode == "3")
toastr.warning(data.resultStatus.Message, "Error");
Metronic.unblockUI();
},
error: function (data) {
toastr.error('An error has occured! \n' + data.resultStatus.Message);
Metronic.unblockUI();
}
});
}
MeetingPollingQuestion = {};
and so forth seem like they're creating globals on the window, but I can't tell for sure. The HTML is missing. What'sMetronic
andtoastr
? Are you using third-party frameworks/libraries here? \$\endgroup\$MeetingPollingQuestion = {};
is a Object initializer for building the object that will be inserted into a database. \$\endgroup\$const
,let
orvar
, that's a global variable attached to the window, which is extremely poor practice and would be flagged by any linter or reviewer, but maybe you have those declarations in an enclosing scope for some reason? Impossible to say because an incomplete snippet was all that was provided. \$\endgroup\$