twinstone / tdi

Turbocharged DOM Infusion
Apache License 2.0
6 stars 1 forks source link

Allow callbacks to be passed in by the default handlers #30

Closed mishal closed 7 years ago

mishal commented 7 years ago

I have to setup custom http header before sending ajax requests. This can be simply implemented by:

$(document).on('tdi:ajax:beforeLinkClick', 'a',  function(e) {
    e.callbacks = e.callbacks || {};
    e.callbacks.beforeStart = function(xhr, settings, ajaxOptions) {
        xhr.setRequestHeader('X-Foobar', 'yes');
        return true; // make the request
    };
});

I did not included any test for this - checked the tests sources but could not find suitable place.

centi commented 7 years ago

I don't want to modify the original Event object. And actually, I don't think this is necessary at all. You could simply prevent the default action for tdi:ajax:beforeLinkClick and send the request yourself:

$(document).on('tdi:ajax:beforeLinkClick', 'a',  function(e) {
    e.preventDefault();

    TDI.Ajax.send(evt.target, {
        beforeStart : function(xhr, settings, ajaxOptions) {
            xhr.setRequestHeader('X-Foobar', 'yes');
            return true; // make the request
        }
    });
});
mishal commented 7 years ago

Ok, thanks.

This could be part of the docs as a simple howto.

centi commented 7 years ago

I moved the docs into the repository. So you can suggest changes yourself.

I've already updated the docs with this example snippet: https://github.com/twinstone/tdi/blob/master/docs/web-page-integration/web-page-integration-js.md

mishal commented 7 years ago

Thanks! Good job :)