Deleting dynamic snippet node
- uestla
- Backer | 799
Hi.
I'm dealing with a kind of specific problem at the moment. I have a few dynamic snippets in the template (wrapped by one static “container” snippet of course) and each of it has link in it which deletes the record it is displaying.
When the server deletes this record via AJAX and invalidates the parent snippet, this deleted record doesn't occur in the payload that is sent (logically – because it's not in the template anymore).
But is there any way how to delete the unoccured node? I tried (besides the invalidation) sending the node ID to the template and delete the node via Javascript (which I don't consider as a clean solution)…
Here's the code which will hopefully say more than my words…
template:
{snippet gallery}
{foreach $files as $file}
{snippet img-$file li}
<h2>{$file->getBasename()}</h2>
{* ... *}
<a n:href="delete!, file => (string) $file" class="ajax">Delete</a>
{/snippet}
{/foreach}
{/snippet}
Presenter:
function handleDelete($file)
{
if (!is_file($file)) {
$this->error('Picture not found.');
}
@unlink($file);
if (!$this->isAjax()) $this->redirect('this');
$this->invalidateControl('gallery');
$this->invalidateControl('dynJs');
$this->template->removeNode = $this->getSnippetId('img-' . $file);
}
As you can see, I'm invalidating here another snippet, which is at the end of a view template:
<script type="text/javascript" n:snippet="dynJs">
layout.update_orders_link = {link updateOrders!};
{ifset $removeNode}$('#' + $.nette.escapeSelector( {$removeNode} )).remove();{/ifset}
</script>
When I display the invalidated and refreshed layout in Chrome DevTools, it seems OK, but it behaves like the dynamic Javascript doesn't execute…
Have you any idea what's wrong or have you any suggestion, please?
EDIT: I'm sorry to forget to mention that the $files
array in the first template code is an array with all files in a current folder
(which after deleting a file doesn't have the deleted file in it anymore
naturally…).
Last edited by uestla (2012-04-13 15:01)
- uestla
- Backer | 799
Thanks! That didn't even occured to me, it needs a small fix anyway:
$this->payload->snippets[ $this->getSnippetId( 'img-' . $file ) ] = '';
Thanks to this I don't need to handle anything special on the client side (that's cool). The sad thing about it anyway is that the node still stays in the template – what I need to do is remove the whole snippet node :-(
I solved it by updating the Jan Marek's jQuery script. The main idea is
that when the snippet content is NULL
, the node is deleted (Nette
never sends NULL
by itself AFAIK, so it should be safe change).
Presenter:
$this->payload->snippets[ $this->getSnippetId( 'img-' . $file ) ] = NULL
jquery.nette.js: https://gist.github.com/2369960#L16
Last edited by uestla (2012-04-13 16:19)