javascript - jQuery Custom Event not triggering in AJAX Success Callback -
i experimenting jquery custom events in application.
in jquery ajax request code below, in success callback trying trigger() custom event
it not seem triggering though. not able done in success callback , needs called elsewhere?
jquery ajax request code
// request task record using ajax request server var jqxhr = $.ajax({ type: 'post', async: false, contenttype: 'application/json; charset=utf-8', datatype: 'json', url: projecttaskmodal.cache.gettaskrecordurlendpoint, data: { action: 'load-task-record', task_id: taskid }, success: function(data) { // non relevant code removed demo // event not being triggered.... // publish 'task-modal-data-loaded' event subscribers $.event.trigger({ type: 'task-modal-data-loaded', source: 'ajax', message: 'task modal object loaded ajax request', time: new date() }); } }); // end ajax request custom event subscriber
$(document).on('task-modal-data-loaded', function(event) { //alert('task modal object loaded'); console.log('custom app events: event: task-modal-data-loaded source: '+event.source+' message: '+event.message+' time: '+event.time); });
from discussion, problem when event triggered handler not yet added dom.
so making sure event handler added before event fired solve probelm
Comments
Post a Comment