Hello,
I’m trying to trigger a JS event whenever a gallery item is removed. I’ve tried:
$( document ).on( 'click', '.acf-gallery-remove', function() { console.log( 'here' ); } );
The above code snippet never triggers. I see that on the field model it has the following event set:
'click .acf-gallery-remove': 'onClickRemove'
Is there a way to get in the middle of that callback or override that callback? I’m just looking to trigger a single event whenever the button is clicked to remove a gallery item.
If there’s a better way to do this, let me know. This is how I was able to do it:
( function( $ ) {
if( 'object' !== typeof acf ) {
return;
}
/**
* Override Gallery Field onClickRemove method.
* This method removes the attachment from the galllery but not the server.
* Runs on media [x] click and "Remove" in gallery sidebar.
*
* @param e Event
* @param $el DOMObject
*
* @return void
*/
acf.models.GalleryField.prototype.onClickRemove = function( e, $el ) {
e.preventDefault();
e.stopPropagation();
let attachment_id = $el.data( 'id' );
if( attachment_id ) {
attachment_id.removeAttachment( attachment_id );
}
/* AJAX to delete the physical attachment. */
};
} )( jQuery );