0

我有一個在瀏覽器中完美工作的Web應用程序 - 無論是在桌面還是移動設備上。 問題是當我嘗試漂亮起來加入:Rails iOS主屏幕Web應用程序 - 無法刪除記錄

<meta name="apple-mobile-web-app-capable" content="yes" /> 

這個偉大的工程太 - 多達我需要在應用程序中刪除記錄點。

我還使用這個偉大的要點,我發現 - https://gist.github.com/1042167停止應用程序轉換成移動Safari瀏覽器:

<script type="text/javascript"> 
    (function(document,navigator,standalone) { 
     // prevents links from apps from oppening in mobile safari 
     // this javascript must be the first script in your <head> 
     if ((standalone in navigator) && navigator[standalone]) { 
      var curnode, location=document.location, stop=/^(a|html)$/i; 
      document.addEventListener('click', function(e) { 
       curnode=e.target; 
       while (!(stop).test(curnode.nodeName)) { 
        curnode=curnode.parentNode; 
       } 
       // Conditions to do this only on links to your own app 
       // if you want all links, use if('href' in curnode) instead. 
       if(
        'href' in curnode && // is a link 
        (chref=curnode.href).replace(location.href,'').indexOf('#') && // is not an anchor 
        ( !(/^[a-z\+\.\-]+:/i).test(chref) ||      // either does not have a proper scheme (relative links) 
         chref.indexOf(location.protocol+'//'+location.host)===0) // or is in the same protocol and domain 
       ) { 
        e.preventDefault(); 
        location.href = curnode.href; 
       } 
      },false); 
     } 
    })(document,window.navigator,'standalone'); 
</script> 

我想知道如果這可以被改變,因此數據的方法=「刪除」,將打得好嗎?在那一刻 - 當我點擊「刪除」時 - 「你確定嗎?」確認框掛起一兩秒鐘,然後把我重新放回到同一個顯示頁面上,但沒有發生刪除...

回答

0

因此,我將假設你寫的是你正在使用的(捆綁)jquery-ujs來處理你的刪除鏈接你這聽起來像你從你的data-method

提做什麼ujs處理程序的刪除鏈接的工作方式是你所期望的不同。下面是the jquery-ujs source的相關位:

// Handles "data-method" on links such as: 
// <a href="https://stackoverflow.com/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> 
    handleMethod: function(link) { 
     var href = rails.href(link), 
     method = link.data('method'), 
     target = link.attr('target'), 
     csrf_token = $('meta[name=csrf-token]').attr('content'), 
     csrf_param = $('meta[name=csrf-param]').attr('content'), 
     form = $('<form method="post" action="' + href + '"></form>'), 
     metadata_input = '<input name="_method" value="' + method + '" type="hidden" />'; 

     if (csrf_param !== undefined && csrf_token !== undefined) { 
     metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />'; 
     } 

     if (target) { form.attr('target', target); } 

     form.hide().append(metadata_input).appendTo('body'); 
     form.submit(); 
    }, 

所以你可以看到,實際上發生的事情是什麼形式被動態創建(具有所需csrf數據),然後提交。

從您鏈接到是不會在這種情況下工作要點您的委派click處理方法,如軌道JS做了return: false上點擊一個data-method屬性,防止您的處理程序無法啓用的鏈接。

最簡單的事情就是根據ujs代碼推出自己的(基於ajax的)刪除處理程序。爲了簡便起見,我採用優jQuery form plugin處理實際提交:

function handleDeleteLink(endpoint){ 
    var confirmed = confirm("Are you sure you want to delete this record?"), 
    csrf_token = $('meta[name=csrf-token]').attr('content'), 
    csrf_param = $('meta[name=csrf-param]').attr('content'), 
    form = $('<form method="post" action="' + endpoint + '"></form>'), 
    metadata_input = '<input name="_method" value="delete" type="hidden" />', 
    deleteOptions = { 
     beforeSubmit: function(){ 
     //handle ajax start 
     }, 
     success: function(el){ 
     //hand success 
     }, 
     error: function(){ 

     } 
    }; 
    if (csrf_param !== undefined && csrf_token !== undefined) { 
     metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />'; 
    } 
    if (confirmed){ 
     form.hide() 
      .append(metadata_input) 
      .appendTo('body') 
      .submit(function(e){ 
      e.preventDefault(); 
      $(this).ajaxSubmit(options); 
      }) 
      .submit(); 
    } 
} 

在你看來只是一類說delete-link的更換你data-method並通過事件委託綁定(或使用不同的數據ATTR如果你喜歡):

$( '身體')上( '點擊', '.delete鏈接',函數(如){ e.preventDefault(); handleDeleteLink(e.target.href);}

+0

感謝您的詳細回覆。我添加了插件,刪除了數據方法,添加了類並單擊處理程序,然後添加了您的函數。不知道我是否需要評論一些相沖突的東西?我可以導航應用程序,但刪除現在只是沒有生氣......我仍然想知道爲什麼在我現有的代碼 - 當它成爲一個iOS homescreened網絡應用程序時,我只會遇到問題。 – ritchielee

+0

當handleDeleteLink()被調用時,表單是否被正確創建/提交?我從應用程序中抽象出了上面的代碼,因此我可能會在概括出來時犯了一個錯誤。你的網絡檢查員應該能夠告訴你事情正在破裂的地方 –

0

問題是location.href = curnode.href;變更請求我nto GET請求,雖然我的行爲與POSTPUT請求不一致。所以,當你有一些途徑,如:

GET  /photos    index 
POST /photos    create 
GET  /photos/new   new 
GET  /photos/:id/edit edit 
GET  /photos/:id   show 
PUT  /photos/:id   update 
DELETE /photos/:id   destroy 

updatedestroy路線最終路由到show。我的hacky(希望是臨時的)解決方案是爲那些正在重新路由的請求創建自定義路由。所以你會補充:

match "update" => "Photos#update", :as => :update_photo 
match "destroy" => "Photos#destroy", :as => :destroy_photo 

我知道,我知道,這不是Rails約定,但它應該工作。

相關問題