I have a pretty simple function as shown below. This deletes a users slip after confirming that's what they want to do (note the custom confirm
). Inside the callback function on confirm, id
gets set correctly the first time, but is never updated after that.
If I console.log
inside of the deleteSlip
function, id
is always correct. If I console.log
inside of the callback, it's always the first id
I send it. I see the same thing when I debug the javascript. The value is correct the first time, but never updates after that.
What do I need to do to get the value inside the callback function to update when it updates in deleteSlip
?
function deleteSlip(id){
// id is correct right here
confirm('This will delete your slip. Are you sure you want to do this?', function(confirmed){
// id here is always the first value from deleteSlip, and never changes
if (confirmed)
{
// do stuff
}
});
}
The confirm
being called is custom and is defined below:
window._originalConfirm = window.confirm;
window.confirm = function(text, cb) {
bootStrapConfirm = function() {
if(! $.fn.modal.Constructor)
return false;
if($('#windowConfirmModal').length == 1)
return true;
$('body').append(' \
<div id="windowConfirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
<div class="modal-body"> \
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
<p> alert text </p> \
</div> \
<div class="modal-footer"> \
<button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">No</button> \
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Yes</button> \
</div> \
</div> \
');
function unbind() {
$("#windowConfirmModal .btn-primary").unbind('click', confirm);
$("#windowConfirmModal .btn-danger").unbind('click', deny);
}
function confirm() { cb(true); delete cb;}
function deny() { cb(false); delete cb;}
$("#windowConfirmModal .btn-primary").bind('click', confirm);
$("#windowConfirmModal .btn-danger").bind('click', deny);
return true;
}
if ( bootStrapConfirm() ){
$('#windowConfirmModal .modal-body p').text(text);
$('#windowConfirmModal').modal();
} else {
console.log('bootstrap was not found');
window._originalConfirm(text);
}
}
deleteSlip
is an onclick
HTML attribute on a button. Like so:
onclick="deleteSlip(1234)"
Again, to reiterate, the id
inside of deleteSlip
is correct. The id
inside of the callback on the confirm
is whatever it gets first and never changes. The callback may be async, but I'm only calling it once at any given time. deleteSlip
is fired on button click.
I posted this question before and was told it was too ambiguous. Please let me know what you need to see to help me work through this. The "do stuff" area has an ajax call, but the value is wrong before that (thus not showing it for people who don't read the entire question).
Update:
It looks like this is an issue with the custom confirm
. I change the custom confirm
to be the following:
window.confirm = function(text, cb) {
bootStrapConfirm = function() {
if(! $.fn.modal.Constructor)
return false;
if($('#windowConfirmModal').length == 1)
return true;
$('body').append(' \
<div id="windowConfirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true"> \
<div class="modal-body"> \
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
<p> alert text </p> \
</div> \
<div class="modal-footer"> \
<button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">No</button> \
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Yes</button> \
</div> \
</div> \
');
return true;
}
if ( bootStrapConfirm() ){
function confirm() { cb(true); }
function deny() { cb(false); }
$("#windowConfirmModal .btn-primary").unbind('click', confirm);
$("#windowConfirmModal .btn-danger").unbind('click', deny);
$("#windowConfirmModal .btn-primary").bind('click', confirm);
$("#windowConfirmModal .btn-danger").bind('click', deny);
$('#windowConfirmModal .modal-body p').text(text);
$('#windowConfirmModal').modal();
} else {
console.log('bootstrap was not found');
window._originalConfirm(text);
}
}
Now when confirm
is called, it calls ALL the id
's it's ever seen. Something I'm not unbind
ing properly?