0

デスクトップとモバイルの両方で、ブラウザーで完全に機能するWebアプリがあります。問題は、次のように追加してきれいにしようとすると発生します。

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

これもうまく機能します-アプリでレコードを削除する必要がある時点まで。

私はまた、私が見つけたこの素晴らしい要点を使用しています-https ://gist.github.com/1042167アプリがモバイルサファリに切り替わるのを止めます:

<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>

data-method = "delete"がうまく機能するように、これを変更できるかどうか疑問に思っていますか?その瞬間-「削除」をクリックすると-「よろしいですか?」削除が行われずに、同じショーページに私をダンプする前に、確認ボックスが1〜2秒間ハングします。

4

2 に答える 2

0

問題は、location.href = curnode.href;リクエストをリクエストに変更することですが、リクエストとリクエスト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

updateとのdestroyルートは に再ルーティングされshowます。私のハッキーな (できれば一時的な) 解決策は、再ルーティングされているリクエストに対してカスタム ルートを作成することです。したがって、次のように追加します。

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

これは Rails の規約ではありませんが、うまくいくはずです。

于 2013-02-08T23:19:47.890 に答える
0

したがって、あなたが書いたことから、(バンドルされた)を使用しjquery-ujsて削除リンクを処理していると仮定します。data-method

ハンドラーが削除リンクに対して機能する方法は、ujs予想とは異なります。ソースの関連ビットは次jquery-ujsとおりです。

// Handles  "data-method" on links such as:
// <a href="/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フォームが動的に (必要なデータを使用して) 作成され、送信されていることが実際に起こっていることがわかります。

clickRails jsは、ハンドラーが起動しない属性をreturn: false持つリンクをクリックするとオンになるため、リンク先の要点からの委任ハンドラーメソッドはこの状況では機能しません。data-method

最も簡単な方法は、コードに基づいて独自の (ajax ベースの) 削除ハンドラーをロールすることujsです。簡潔にするために、実際の送信を処理するために優れたjQuery フォーム プラグインを使用しています。

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();
    }
}

あなたのビューでは、あなたdata-methodを say のクラスに置き換えて、delete-linkイベント委任を介してバインドします (または、必要に応じて別のデータ属性を使用します)。

$('body').on('click', '.delete-link', function(e){ e.preventDefault(); handleDeleteLink(e.target.href); }

于 2012-11-01T15:22:57.510 に答える