arvgta / ajaxify

Ajaxify - An Ajax Plugin
https://4nf.org/
275 stars 124 forks source link

Request response before applying styles #43

Closed Shade- closed 9 years ago

Shade- commented 9 years ago

Hi, I would like to be able to read the ajax response when targeting the pronto.load event. This is the workflow I would like to achieve:

Currently I am targeting the pronto.request event (requestDelay = 300 to allow css transitions), firing an animation basically onClick, without checking for the actual response. But I would like to check for the response before triggering any animation; is there a variable or something?

arvgta commented 9 years ago

Hi,

thanks for opening this interesting issue!

Again, I would be targetting a solution, that would be best for as many users as possible. Here's my proposal:

I'll add a public method to the $.getPage sub-plugin, analogue to the existing ones, that can be addressed like this:

$.getPage('x')

That would return the last xhr object at any time the user wants.

In your use case, you could simply call the new public method from within the pronto event handler.

What do you think?.

Shade- commented 9 years ago

Seems the best solution to me.

arvgta commented 9 years ago

I'm glad you agree :)

I'll implement it asap...

Shade- commented 9 years ago

Maybe you should move the pronto.load event before the _render2() call wrapped in the setTimeout() function, or create a new event right there: pronto.request is triggered before the request, pronto.load after an eventual delay set with requestDelay. This is useful when applying custom animations to page transitions.

arvgta commented 9 years ago

Thanks for the new idea!

I'd favourise a new event, also for backwards compatibility. What should be call it?

Shade- commented 9 years ago

pronto.beforeload?

arvgta commented 9 years ago

So I'll change _render

to this:

        function _render(e, doPush, mode) {
            if (requestTimer !== null) {
                clearTimeout(requestTimer);
                requestTimer = null;
            }

            requestTimer = setTimeout(function () {
                 $window.trigger("pronto.beforeload", e);
                _render2(e, doPush, mode);
            }, requestDelay);
        }

Would that be alright?

Shade- commented 9 years ago

Maybe out of the setTimeout, right before it. This way, we can act before the new page rendering if a requestDelay is set.

 function _render(e, doPush, mode) {
        if (requestTimer !== null) {
            clearTimeout(requestTimer);
            requestTimer = null;
        }

        $window.trigger("pronto.beforeload", e);
        requestTimer = setTimeout(function () {
            _render2(e, doPush, mode);
        }, requestDelay);
    }
arvgta commented 9 years ago

Thanks! Like this:

        function _render(e, doPush, mode) {
            if (requestTimer !== null) {
                clearTimeout(requestTimer);
                requestTimer = null;
            }

            $window.trigger("pronto.beforeload", e);

            requestTimer = setTimeout(function () {
                _render2(e, doPush, mode);
            }, requestDelay);
        }

?

Shade- commented 9 years ago

Yes. It should work fine once the public method to access the response is ready.

arvgta commented 9 years ago

Yes, I'll make the public method right away and then provide a test file - sec

arvgta commented 9 years ago

Done

or here:

@4nf.org

Changes

Shade- commented 9 years ago

Awesome, it works like a charm. Thank you arvgta, and good luck with future updates! I will suggest other features if I will find them necessary or useful.