Separating AJAX requests and success callbacks in jQuery -
i've defined function capture mouse clicks on links data-remote
tag follows.
// global ajax request handler $(document).ready(function() { $(document).on('click', 'a[data-remote]', function(event) { var target = $(event.target); var url = $(this).data('url'); var method = $(this).data('method'); $.ajax(url, {type: method, data: $(this).data()}) .done(function(data){ target.trigger('complete', data); }) .fail(function(data) { target.trigger('failure', data); }); }); });
the function captures done
, fail
events , proceeds trigger new custom event (complete
or failure
) on element clicked start whole thing off.
elsewhere, it's possible like...
$('#list').on('complete', 'a[data-remote]', function() { $(this).hide(); });
...to hide element clicked, providing ajax call success.
it feels capturing event , triggering new 1 bit convoluted. jquery provide easier way this? i've looked around ajaxsuccess
event...
$('#list').on('ajaxsuccess', 'a[data-remote]', function() { $(this).hide(); });
...but doesn't trigger. there obvious reason why?
Comments
Post a Comment