2012-07-03 150 views
0

我爲我的路由使用了註釋。在我index.twig模板我寫了一些JQuery的像Symfony2路由註釋和JavaScript

$(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 callsone被調用。

該集團是在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> 

誰能giude我怎麼給我從我的JS裏面路徑URL。我不想使用路由文件。

由於

回答

2

當我需要從控制器/視圖的數據傳遞到在相關的HTML標籤我通常設置數據屬性的JavaScript。例如,如果我需要一個路線AJAX請求,我會寫:

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

,然後用訪問:

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

還有一包用動態JS做更復雜的事情路由https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

編輯: 針對您的特殊情況下,你可以設置集團的集裝箱航線上的數據時,<ul>

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

,然後從你的JS文件中,可以參考這些數據屬性:

$.ajax({ 
    type: "POST", 
    data: data, 
    url:$('#portfolioContainer').data('ajax-trash'), 
    cache: false, 
    success: function(data) { 
     document.location.reload(true); 
    }); 
+0

的問題是,我有兩個組,我可以拖動像分揀組,當我有點任何一組它應該調用一個方法,如果我垃圾組中的任何一組應該調用另一個操作,那麼這兩個組都可以傳輸。認爲組作爲任何html標籤 –

+0

這個想法是一樣的。你會將生成的'path'存儲在* some *標籤中。然後從你的JS文件中,你可以根據ID來訪問它。這只是一種將生成的URL轉換爲javascript的方法。如果你需要更復雜的JS路線,那麼我會看看我鏈接的包。 – MDrollette

+0

但我移動的元素已經有了id –