Refresh just a custom block, not the entire page.

Permalink
I have a custom block that fetches and displays network monitoring data. I would like to be able to refresh (every 5 minutes or so) just the one custom block and not the entire page. I tried using the meta-refresh tag in the header of the page and got all buggered up when editing the content from the dashboard. One option is to check to see if in dashboard mode and not output the meta-refresh tag, else insert the meta-refresh tag into the header and meta-refresh the entire page.

I was hoping there was more elegant way to do this.

Thanks in advance... JK

nteaviation
View Replies:
marcandre replied on at Permalink Reply
marcandre
Your approach for the "Edit mode" is right, but for the refresh of the block you can do that very elegantly through a piece of JavaScript and jQuery.

If nobody gets here first with an example, I'll give you as soon as I get back to work.
JohntheFish replied on at Permalink Reply
JohntheFish
A bit of jQuery, in view mode only, that does a 'load' to the div the block displays in.

The load calls a tool in C5 that renders just the block. You will need to do this in the context of the page, even though it is just the one block you render in the tool.
ScottC replied on at Permalink Reply
ScottC
you'd want to wire up a window.setTimeOut to a function that does an ajax request, if you are trying to get raw data its probably best to wire it up to a tools file if you want to mix in html.
nteaviation replied on at Permalink Reply
nteaviation
Thanks! Excellent ideas. I envision having a "Time until refresh (in seconds)" option in the setup/block edit options allowing me to control the persistant variable.

Any examples would be greatly appreciated.
JohntheFish replied on at Permalink Reply
JohntheFish
The score blocks for both of my games addons use a similar technique.
Metaglyphics replied on at Permalink Reply
Did anyone ever figure this out? I'm also trying to figure out a way to have one of the blocks on my site refresh every few seconds.

I was thinking of a jquery refresh, but I'm not sure of the syntax to load just the specific block, rather than the entire page:

window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshBlock();",300);
    }
    function refreshBlock()
    {
       $('.advertisements').load("???");
    }


I was hoping there was some syntax to use where I have the "???" that would allow me to specify the C5 block.

Thanks
ScottC replied on at Permalink Reply
ScottC
It'd have to call out to something that returns a new src or similar to replace the existing ad for. I"m not sure that there is anything out there that allows this but it wouldn't be that hard to create something to do this, though inevitably it'd be a day worth of work,
JohntheFish replied on at Permalink Reply
JohntheFish
My addon 'Blocks by AJAX' that can do this sort of thing by providing a template for many popular blocks.
http://www.concrete5.org/marketplace/addons/blocks-by-ajax/...

(Or just putting the block in a stack and loading the stack by AJAX)

What you actually need is a slight variation on the standard templates that triggers the AJAX load on a repeating timer rather than the default 'when visible' event. Facilities to do this are built in to the addon so doing what you want is only a case of copying a template, changing a line of php and adding the timer JavaScript to trigger it.

Guidance on doing this is in the block documentation.

An example of a more complex 'Blocks by AJAX' load can be found in 'Quick Log View'.
http://www.concrete5.org/marketplace/addons/quick-log-view/....