0

我正在为我的路由使用注释。在我的 index.twig 模板中,我写了一些类似的 JQuery

 $(document).ready(function(){
    $('#sortable').NestedSortable(
        {
            accept: 'sort',
            noNestingClass: "no-children",
            helperclass: 'helper',
            autoScroll: true,
            onChange: function(serialized) {
            },
            onStop : function(){
                var element_id = $(this).attr("id");
                var parent_id = $(this).parent().attr("id");
                var prev_sibling_id = $(this).prev().attr("id");
 if(prev_sibling_id=='trash'){
    var data = {PID:element_id};
    $.ajax({
        type: "POST",
        data: data,
        url:"{{ path('v2_pm_patentgroups_trash') }}",
        cache: false,
        success: function(data) {
            document.location.reload(true);
        });
}
else if(parent_id=='sortable'){
     var p_sibling = $(this).prev().attr("value");
     if(p_sibling == null){var p_sibling = 0;}
     var n_sibling = $(this).next().attr("value");
     if(n_sibling == null){var n_sibling = 0;}
     var order = (p_sibling + n_sibling)/2;
     var data = {ID:element_id, ORD:order};
       $.ajax({
         type: "POST",
         data: data,
         url:"{{ path('v2_pm_patentgroups_sortgroups') }}",
         cache: false
       });
}

现在您看到有两个 ajax 调用,一个在组被丢弃时调用,另一个在对组进行排序时调用。

该组是我的树枝文件中的 li 标签

 <li id="{{ portfolio_group.id }}" class="sort group" value={{ portfolio_group.order }}>
    <span class="drag-image groupimage">&nbsp;</span>
    <a class='expand'>{{ portfolio_group.name }}</a>
    <a class="button3" href="{{ path('v2_pm_patentgroups_edit', { 'patentgroupId': portfolio_group.id }) }}" ><span> Edit </span></a> 
            <a class="button3" href="{{ path('v2_pm_patentgroups_delete', { 'patentgroupId': portfolio_group.id }) }}" ><span> Delete </span></a>
    <hr class="separator">
 </li>

任何人都可以指导我如何从我的 JS 中给出 url 的路径。我不想使用路由文件。

谢谢

4

1 回答 1

2

When I need to pass data from the controller/view to the javascript I usually set data attributes on the relevant HTML tags. For example, if I need a route for an AJAX request I will write:

<a href="#updateTarget" class="ajaxTrigger" data-ajax-route="{{ path('my_ajax_route') }}">click here for ajax</a>

and then access it with:

$('.ajaxTrigger').on('click', function(){
  $.getJSON($(this).data('ajax-route'), function(response) {
    // do something with response
  });
 });

There is also a bundle to do much more sophisticated things with dynamic JS routing https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

edit: For your specific case you could set the route data on the container of the group, the <ul>

<ul id="portfolioContainer" 
    data-ajax-trash="{{ path('v2_pm_patentgroups_sortgroups') }}" 
    data-ajax-sort="{{ path('v2_pm_patentgroups_sortgroups') }}">

and then from your JS file you would refer to those data attributes:

$.ajax({
    type: "POST",
    data: data,
    url:$('#portfolioContainer').data('ajax-trash'),
    cache: false,
    success: function(data) {
        document.location.reload(true);
    });
于 2012-07-03T21:59:43.850 回答