简体   繁体   English

在确认对话框中重定向到模板之前执行一个操作

[英]Execute an action before redirecting to the template in the confirm dialog

The first field is the title of critic, the second field is the content of critic. 第一个字段是评论家的标题,第二个字段是评论家的内容。 When I write something in the title field automatically creates the object Critic in the db. 当我在标题字段中写一些东西时,会自动在数据库中创建对象Critic。 In this situation I have a new row with the new critic but in the fileld of content the value is null. 在这种情况下,我将在新行中添加新评论者,但在内容文件中,该值为null。 In this situation appears a confirm dialog. 在这种情况下,将显示一个确认对话框。

When the confirm dialog appears, what should I do to click the "ok" button and, apart from being redirected to the new template, executing the eliminarCriticaAction of the Controller. 当出现确认对话框时,我该怎么做才能单击“确定”按钮,并且除了被重定向到新模板之外,还执行Controller的eliminarCriticaAction。

$('a').on('click', function(e) {
        if( ! $('#criTitulo').val() || ! $('#criContenido').val() ) {
            if ( ! $('#criTitulo').val() && $('#criContenido').val() ) {
                if(! window.confirm( 'Falta el titulo' )) {
                    e.preventDefault();
                }               
            }
            else if ( ! $('#criContenido').val() && $('#criTitulo').val() ) {
                return confirm('Falta el contenido');
            }   
        }
    });

Delete action of Controller: 删除控制器动作:

public function eliminarCriticaAction($pysStr)
{
    $em = $this->getDoctrine()->getManager();
    $pys = $em->getRepository('PYSBundle:Pys')->findPys($pysStr);
    $usuario = $this->get('security.context')->getToken()->getUser();
    $critica = $em->getRepository('UsuarioBundle:Usuario')->findCritica($usuario, $pys);

    if(!$critica) 
    {
        throw new AccessDeniedException("No hay ninguna crítica que borrar");
    }

    $em->remove($critica);

    $em->flush();

}

EDIT 编辑

$('a').on('click', function(e) {
    var titulo = $('#criTitulo').val(), contenido = $('#criContenido').val();
    console.log(titulo);
    console.log(contenido);
    if ( ( titulo && !contenido ) || ( !titulo && contenido ) ) {
        e.preventDefault();
        console.log('Link clicked !');
        if (window.confirm( 'Falta el titulo' )) {
            $.get(Routing.generate('eliminar_critica.' + $('html').attr('lang'), { "_locale": $('html').attr('lang'), "pysStr": $('section').attr('pelicula') }));
            window.location.href = $(e.target).attr('href');
        }
    }
});

It is unclear to me what the route to your delete controller is. 我不清楚到您的删除控制器的路由是什么。 I will assume "/Pys/{pysStr}" with route name "my_pys". 我将假定路由名称为“ my_pys”的“ / Pys / {pysStr}”。 Also your current "pysStr" should be available as variable in your twig template. 另外,您当前的“ pysStr”应该在树枝模板中作为变量提供。 Assuming in your display controller you put: 假设在显示控制器中放置:

'currentPysStr' => $pysStr    (put this in the render method are argument)

$('a').on('click', function(e) {
    if( ! $('#criTitulo').val() || ! $('#criContenido').val() ) {
        if ( ! $('#criTitulo').val() && $('#criContenido').val() ) {
            if(! window.confirm( 'Falta el titulo' )) {
                e.preventDefault();
            } else {
                $.get({{ path('my_pys', {'pysStr': currentPysStr}) }})
            }
        }
        else if ( ! $('#criContenido').val() && $('#criTitulo').val() ) {
            return confirm('Falta el contenido');
        }   
    }
});

Then in your delete controller you will have the current PysStr and this controller will only be called when you press OK in the confirmation dialog. 然后,在删除控制器中,您将拥有当前的PysStr,只有在确认对话框中按“确定”时,才会调用此控制器。

The same as in your order question you have a choice if you want to put this script in a twig template or use a global variable. 与您在订单问题中一样,您可以选择是否将此脚本放入树枝模板或使用全局变量。 ( https://stackoverflow.com/questions/18035337/translate-the-jeditable-plugins-attributes/18035436#18035436 ) https://stackoverflow.com/questions/18035337/translate-the-jeditable-plugins-attributes/18035436#18035436

EDIT: (after chat) 编辑:(聊天后)

$('a').on('click', function(e) {
    var titulo = $('#criTitulo').val(),
        contenido = $('#criContenido').val();
    console.log(titulo);
    console.log(contenido);
    // Requested: XOR
    // One of the values must be set, the other one must not be set
    if ( ( titulo && !contenido ) || ( !titulo && contenido ) ) {
            e.preventDefault();
            console.log('Link clicked !');
            if (window.confirm( 'Falta el titulo' )) {
                var ajax;
                var url = Routing.generate('eliminar_critica.' + $('html').attr('lang'), { "_locale": $('html').attr('lang'), "pysStr": $('section').attr('pelicula') });
                console.log(url); // This is just here for debugging purposes
                ajax = $.get(url);
                ajax.done(function() {
                    window.location.href = $(e.target).attr('href');
                });
            }
    }
});

Placing window.location.href in done() guarantees the request was made before the page refresh window.location.href放置在done()确保在刷新页面之前发出请求

eliminarCriticaAction should give a Response that everything went on (2**) response. eliminarCriticaAction应该给出一个响应,表示一切都eliminarCriticaAction (2 **)响应。 204 is appropriate like this: 204是这样的:

return new response('', 204); // 204: No Content

Take a look at the FOSJsRoutingBundle . 看看FOSJsRoutingBundle It gives you the possibility to use your routes in JavaScript as well. 它也使您可以在JavaScript中使用路由。

After installing the bundle, modify your Controller annotation : 安装捆绑软件后,修改Controller annotation

eliminar_critica:
    locales: { es: "/eliminar-critica/{pysStr}/", en: "/delete-critic/{pysStr}/" }
    defaults: { _controller: UsuarioBundle:Default:eliminarCritica }
    options:
        expose: true

After that you can use this code of JavaScript to access the route: 之后,您可以使用以下JavaScript代码访问路由:

Routing.generate('eliminar_critica', { pysStr: 10 });
// will result in /eliminar-critica/10/

$.get(Routing.generate('eliminar_critica', { pysStr: 10 }));
// will call /eliminar-critica/10/ without redirecting your browser

For further reading you should read about jQuery and AJAX 为了进一步阅读,您应该阅读有关jQueryAJAX的信息。


In your JavaScript: 在您的JavaScript中:

$('a').on('click', function(e) {
    if( ! $('#criTitulo').val() || ! $('#criContenido').val() ) {
        if ( ! $('#criTitulo').val() && $('#criContenido').val() ) {
            if(! window.confirm( 'Falta el titulo' )) {
                e.preventDefault();
            } else {
                // insert this line: (optional add a callback)
                $.get(Routing.generate('eliminar_critica', { pysStr: 10 }));
            }
        }
        else if ( ! $('#criContenido').val() && $('#criTitulo').val() ) {
            return confirm('Falta el contenido');
        }   
    }
});

For a nice callback like 对于像这样的不错的回调

Your Entity has been deleted! 您的实体已被删除!

Take a look at the jQuery get() method here . 这里看看jQuery get() 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM