eldarion / eldarion-ajax

a library for adding declarative ajax functionality to your website
BSD 3-Clause "New" or "Revised" License
758 stars 153 forks source link

Raise some custom events #17

Closed jakejscott closed 11 years ago

jakejscott commented 11 years ago

Hi just wondering if you'd be keen to add a custom event that fires at the end of the processData method.

Something like this

$(document).trigger('Ajax:success', [ data, $this ]);

That way I could listen for that event and do any minor things not covered by the library? Basically I want to be able to initialise a jQuery plugin on the newly append elements that have come back from the server. I could use jQuery livequery but everyone frowns on that :)

This is the listener:

    lazyloadimages();

    $(document).bind('Ajax:success', function(evt, data, $el) {
        if ($el.attr('data-replace-closest') == '.load-button') {
            lazyloadimages();
        }
    });

    function lazyloadimages() {
        $("img.lazy").lazyload({ effect : "fadeIn", event : 'scrollstop' });
    }

I was also playing around with the idea of the server being able to send back multiple fragments, each fragment consisting of a command, html and a selector. Would add some pretty cool scenarios for example not having to use refresh:

In C# on the server it would look something like this:

            /* JUST thinking out loud here
            // append
            // replace
            // remove (just send the selector)
            // refresh (send a url for the ui to requery)
            // we could return fragments like this:

            return Json(new 
            {
                fragments = new [] {
                    new {
                        command = "append",
                        selector = ".result-list",
                        html = "<p>Take out the trash</p>"
                    },
                    new {
                        command = "replace",
                        selector = ".task-counter",
                        html = "<span class='count'>5</span>"
                    }
                }
            });
            */
paltman commented 11 years ago

@superlogical love this idea. it's now on the roadmap for 1.0 release targeting before the end of the month