Skip to content

Commit 4d02853

Browse files
committed
Fixed data-type typo in attribute definition. Added parameter data to callbacks. Changed how callbacks are added (delete_callback is added even though a callback was not defined). Extracted variables for deleteBtn and cancelBtn. delete_callback checks for a valid function as callback did.
1 parent aee1062 commit 4d02853

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ on any links or buttons that have the 'delete' class on them.
2929
```
3030

3131
```html
32-
<a href="server.php" class="delete" data_type="post">Delete</a>
32+
<a href="server.php" class="delete" data-type="post">Delete</a>
3333
```
3434

3535
Initialise the plugin either in <script></script> tags on the html page or in an external .js script.
@@ -41,7 +41,7 @@ $( document ).ready( function( )
4141
} );
4242
```
4343

44-
Notice the 'data_type' attribute on the link. The plugin will use this attribute if set to show a custom delete message. For example,
44+
Notice the 'data-type' attribute on the link. The plugin will use this attribute if set to show a custom delete message. For example,
4545

4646
Heading: 'Delete Post'
4747

@@ -93,12 +93,18 @@ Used if heading & message are not provided
9393
Will fire if responding to a button click that has no href attribute.
9494

9595
Use this callback to do any deletions from a button click.
96+
Parameters:
97+
* data (data.originalObject contains the originally clicked object)
9698

9799
### delete_callback
98100
Will fire when the delete button is clicked and a handler is provided.
101+
Parameters:
102+
* data (data.originalObject contains the originally clicked object)
99103

100104
### cancel_callback
101105
Will fire when the cancel button is clicked and a handler is provided.
106+
Parameters:
107+
* data (data.originalObject contains the originally clicked object)
102108

103109

104110
## License

bootstrap-confirm-delete.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,41 @@
4545
$( '#bootstrap-confirm-dialog-text' ).html( plugin.settings.message );
4646
$( '#bootstrap-confirm-dialog' ).modal( 'toggle' );
4747

48+
var deleteBtn = $( 'a#bootstrap-confirm-dialog-delete-btn' );
49+
var cancelBtn = $( 'a#bootstrap-confirm-dialog-cancel-delete-btn' );
50+
var hasCallback = false;
4851
if ( null !== plugin.settings.callback )
4952
{
50-
$( '#bootstrap-confirm-dialog-delete-btn' ).attr( 'data-dismiss', 'modal' );
51-
5253
if ( $.isFunction( plugin.settings.callback ) )
5354
{
54-
var deleteBtn = $( 'a#bootstrap-confirm-dialog-delete-btn' );
55-
56-
deleteBtn.off().on( 'click', { originalObject: $( this ) }, plugin.settings.callback );
57-
58-
if ( null !== plugin.settings.delete_callback )
59-
{
60-
deleteBtn.off().on( 'click', { originalObject: $( this ) }, plugin.settings.delete_callback );
61-
}
55+
deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.callback );
56+
hasCallback = true;
6257
}
6358
else
6459
{
6560
console.log( plugin.settings.callback + ' is not a valid callback' );
6661
}
6762
}
68-
else if ( '' !== event.currentTarget.href )
63+
if ( null !== plugin.settings.delete_callback )
64+
{
65+
if ( $.isFunction( plugin.settings.delete_callback ) )
66+
{
67+
deleteBtn.attr( 'data-dismiss', 'modal' ).off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.delete_callback );
68+
hasCallback = true;
69+
}
70+
else
71+
{
72+
console.log( plugin.settings.delete_callback + ' is not a valid callback' );
73+
}
74+
}
75+
if ( !hasCallback && '' !== event.currentTarget.href )
6976
{
70-
$( 'a#bootstrap-confirm-dialog-delete-btn' ).attr( 'href', event.currentTarget.href );
77+
deleteBtn.attr( 'href', event.currentTarget.href );
7178
}
7279

7380
if ( null !== plugin.settings.cancel_callback )
7481
{
75-
$( '#bootstrap-confirm-dialog-cancel-delete-btn' ).off().on( 'click', { originalObject: $( this ) }, plugin.settings.cancel_callback );
82+
cancelBtn.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', { originalObject: $( this ) }, plugin.settings.cancel_callback );
7683
}
7784
};
7885
};
@@ -91,7 +98,7 @@
9198
var plugin = new bootstrap_confirm_delete( this, options );
9299

93100
element.data( 'bootstrap_confirm_delete', plugin );
94-
element.on( 'click', plugin.onDelete );
101+
element.off('.bs-confirm-delete').on( 'click.bs-confirm-delete', plugin.onDelete );
95102

96103
return plugin;
97104
} );

0 commit comments

Comments
 (0)