vendredi 28 novembre 2014

Improving Javascript and Modal HTML


I have a bootstrap modal that was modified to be displayed to get a confirmation when a user clicks on a button to delete the item. After thinking about it I want to be able to use the same bootstrap modal template when I want to get confirmation from the user when they try to create a new item or update and item.


What I would like to do is be able to use the same modal template but just modify the text with jQuery based on the situation needed (create, update, delete). What I think would be easier is to also have a javascript function for create, update, delete. I am also using the gritter jQuery plugin and have duplicating code I would like to move out.


What can I do to my modal HTML to work with this. As well as what can I do to my javascript so that it can know if this was coming from a create, update, delete method. Also how do I modify the gritter code so that I don't have duplicating code. The time should be the same as well as the sticky and image.



<!-- Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure?</p>
</div>
<div class="modal-footer">
<button type="button" id="deleteModalCancel" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" id="deleteModalConfirm" class="btn btn-primary">Yes</button>
</div>
</div><!-- modal-content -->
</div><!-- modal-dialog -->
</div><!-- modal -->


$(document).ready(function(){
$(function()
{
var $modal = $('#deleteModal');

$('.js-ajax-delete').on('click', function(e)
{
e.preventDefault();
var deleteUrl = $(this).attr('href');
$modal.data('delete-url', deleteUrl);
$modal.modal('show');

$('#deleteModalConfirm').on('click', function(e)
{
$modal.modal('hide');
var deleteUrl = $modal.data('delete-url')
submitDeleteRequest(deleteUrl);
});
});
});
});

function submitDeleteRequest(route) {
$.post(route, {"_method" : "DELETE"}, function(response) {
if (response.errors > 0) {
$.gritter.add({
title: 'This is a regular notice!',
text: 'Deletion was successful.',
class_name: 'growl-error',
image: 'images/screen.png',
sticky: false,
time: ''
});
} else {
$.gritter.add({
title: 'This is a regular notice!',
text: 'Deletion was successful.',
class_name: 'growl-success',
image: 'images/screen.png',
sticky: false,
time: ''
});
}
})
}




Aucun commentaire:

Enregistrer un commentaire