Skip to content

Instantly share code, notes, and snippets.

@taulutwashi
Created December 9, 2018 18:58
Show Gist options
  • Save taulutwashi/e35359612e85b0ba6e4ca59e6c22b8c4 to your computer and use it in GitHub Desktop.
Save taulutwashi/e35359612e85b0ba6e4ca59e6c22b8c4 to your computer and use it in GitHub Desktop.
This is a simple MVC pattern template for jquery
// This Is Controller for contact Model and View
const Controller = {
// Initiate Model and View method
init: function() {
Model.init();
View.init();
},
// check submisstion status
checkSubmissionsStatus: function(_that) {
Model.checkSubmissions().then(function(response) {
if (response.status == 200) {
View.renderSubmissionButton(false, response.status_id, response.job_id, response.overviewRoute);
}
}).catch(function(reason, exception) {
let message = Controller.handleErrorType(reason, exception);
swal('Error fetching checking status: ', message, 'error')
});
},
// delete signle submission
deleteSubmission: function(event) {
let id = event.data('id');
Model.deleteSubmission(id).then(function(response) {
if (response.status === 200) {
$('#bs4-table-submissions').DataTable().row(event.parents('tr')).remove().draw();
View.renderSubmissionButton(true);
swal('Deleted', 'The submission has been deleted.', 'success');
}
}).catch(function(reason, exception) {
let message = Controller.handleErrorType(reason, exception);
swal('Error deleting', message, 'error')
});
},
// Handle All types of error
handleErrorType: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connection.\n Verify your network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Request timed out';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else if (jqXHR.status == 422) {
$.each(jqXHR.responseJSON.errors, function(key, value) {
msg += '<li>' + value + '</li>';
});
} else {
msg = 'Error: \n' + jqXHR.responseText;
}
return msg;
}
};
// This is View for handle handle View File
const View = {
//initially load all class, id and events.
init: function() {
let assignmentContent = $('#assignment-content');
let cardCheck = assignmentContent.find('#card-checking');
if (!cardCheck.length == 0) {
let _that = cardCheck
Controller.checkSubmissionsStatus(_that);
}
assignmentContent.on('click', '.delete-submission', function(e) {
e.preventDefault();
let _that = $(this);
View.deleteSubmission(_that);
});
},
renderSubmissionButton: function(check) {
$("#card-checking").html('<div class="card"><a href="javascript:void(0)" id="checkSubmissions" class="btn btn-info btn-outline btn-lg btn-block float-right">Start checking submissions </a> </div>')
},
// delete single submission swal view
deleteSubmission: function(_that) {
swal({
title: 'Delete submission?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Delete submission',
closeOnConfirm: false,
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve, reject) {
Controller.deleteSubmission(_that);
});
return false;
}
})
},
};
// This Is Model for handle Database related issue
const Model = {
init: function() {},
// check submission model status
checkSubmissions: function() {
return new Promise(function(resolve, reject) {
$.ajax({
url: location.origin + '/checking_submission_status',
type: "get",
success: function(data) {
resolve(data);
},
error: function(xhr) {
reject(xhr)
}
});
});
},
// Delete Single Sumission
deleteSubmission: function(data) {
return new Promise(function(resolve, reject) {
$.ajax({
url: window.location.origin + '/dash/course/assignment/deleteSubmission/' + data,
type: "post",
success: function(data) {
resolve(data);
},
error: function(xhr) {
reject(xhr)
}
});
});
},
};
SubmissionsController.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment