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

Refresh a data-refresh-url item #57

Open Gelurban opened 11 years ago

Gelurban commented 11 years ago

Hello,

I'd like to know how could I refresh the following div by clicking on a button or anchor. I tried the following code:

< div id="my-div" data-refresh-url="info">

< a data-refresh="#my-div" > Refresh! < /a>

Is it possible to request an item refresh without sending a Get request to the page?

Thanks in advance!

lonnieezell commented 9 years ago

This is exactly what I'm trying to do, also. Did you ever get this working?

I would love to have a static html page (or a cached page on the server) being sent to the client that only refreshes one or two dynamic portions. This would work great when a lot of content is static, but you wan to have different links if user is signed in, and have the page automatically refresh items on page load.

oppianmatt commented 9 years ago

Couldn't you do something like?

< div id="my-div" data-refresh-url="info">

< a class="refresh-btn" data-refresh="#my-div" > Refresh! < /a>

<script>
$(document).on('click', '.refresh-btn', function() {
  $('#my-div').trigger('eldarion-ajax:success');
});
</script>

It's saying when you click the btn, trigger the success event (which has the refresh handler attached).

See in eldarion-ajax-handlers.js file:

$(document).on('eldarion-ajax:success', '[data-refresh]', Handlers.prototype.refresh);
    Handlers.prototype.refresh = function(e, $el) {
        $.each($($el.data('refresh')), function(index, value) {
            $.getJSON($(value).data('refresh-url'), function(data) {
                $(value).replaceWith(data.html);
            });
        });
    };

You can put that in a separate js file. Infact I have another js full of our eldarion custom plugins.