2017-09-29 5 views
2

現在、私はこの関数の中で、ajax呼び出しがあり、応答を返すajax呼び出しの後に、私はajax関数を追加したいが、私が定義しなければならない場合、私には非難されるjavascriptファイル構造を持っているすべてのAjaxレスポンスのタイプごとに1つずつ、それは多くのスペースを消費するので、このajax関数を呼び出す関数を作る必要がありますが、私はこの関数をどこに置くべきかわかりません。ここに私のコードは、私はちょうどPOST機能にsuccesscallbackerrorcallbackを追加deprecated javascript関数内で関数を作成

return Component.extend({ 
    defaults: { 
     template: 'Icube_Snap/payment/snap' 
    }, 
    redirectAfterPlaceOrder: false, 
    afterPlaceOrder: function() { 
     $.getScript(js, function() { 
      $.ajax({ 
       type: 'post', 
       url: url.build('snap/payment/redirect'), 
       cache: false, 
       success: function(data) { 
        var token = data; 
        var methods = []; 
        var methodSnap = $('input[name=snap-method]:checked').val(); 

        snap.pay(token, { 
         enabledPayments: methods, 
         onSuccess: function(result) { 
          $.ajax({ // <-- this ajax needs to be inside function with parameter 
           type: 'post', 
           url: url.build('custom/message/post'), 
           cache: false, 
           param: { 
            id: resut.id, 
            message: result.message 
           } 
           success: function(data) { 

           } 
          }); 
         }, 
         onPending: function(result) { 
          $.ajax({ // <-- this ajax needs to be inside function with parameter 
           type: 'post', 
           url: url.build('custom/message/post'), 
           cache: false, 
           param: { 
            id: resut.id, 
            message: result.message 
           } 
           success: function(data) { 

           } 
          }); 
         }, 
         onError: function(result) { 
          $.ajax({ // <-- this ajax needs to be inside function with parameter 
           type: 'post', 
           url: url.build('custom/message/post'), 
           cache: false, 
           param: { 
            id: resut.id, 
            message: result.message 
           } 
           success: function(data) { 

           } 
          }); 
         }, 
         onClose: function() { 
          $.ajax({ // <-- this ajax needs to be inside function with parameter 
           type: 'post', 
           url: url.build('custom/message/post'), 
           cache: false, 
           param: { 
            id: resut.id, 
            message: result.message 
           } 
           success: function(data) { 

           } 
          }); 
         } 
        }); 
       } 
      }); 
     }); 
    } 
}); 

答えて

0

です。しかし、あなたが望むのであれば、functionsを無視して、functionの中にsuccesserrorという機能を実装し、コールバックを使用することはできません。

return Component.extend({ 
 
    defaults: { 
 
    template: 'Icube_Snap/payment/snap' 
 
    }, 
 
    redirectAfterPlaceOrder: false, 
 
    afterPlaceOrder: function() { 
 
    $.getScript(js, function() { 
 
     $.ajax({ 
 
     type: 'post', 
 
     url: url.build('snap/payment/redirect'), 
 
     cache: false, 
 
     success: function(data) { 
 
      var token = data; 
 
      var methods = []; 
 
      var methodSnap = $('input[name=snap-method]:checked').val(); 
 

 
      //Define a function to send the POST request here. 
 
      ////////////////////////////////////////////////// 
 

 
      var sendPayment = function(param, successcallback, errorcallback) { 
 
      $.ajax({ // <-- this ajax needs to be inside function with parameter 
 
       type: 'post', 
 
       url: url.build('custom/message/post'), 
 
       cache: false, 
 
       param: { 
 
       id: param.id, 
 
       message: param.message 
 
       } 
 
       success: function(data) { 
 
       successcallback(data); 
 
       }, 
 
       error: function(error) { 
 
       errorcallback(error); 
 
       } 
 
      }); 
 
      }; 
 

 
      snap.pay(token, { 
 
      enabledPayments: methods, 
 
      onSuccess: function(result) { 
 

 
       //Call sendPayment method and you can 
 
       //pass whatever you want. 
 

 
       sendPayment(result, function() { 
 
       //Call when success 
 
       }, function() { 
 
       //Call when error 
 
       }); 
 
      }, 
 
      onPending: function(result) { 
 
       sendPayment(result, function() { 
 
       //Call when success 
 
       }, function() { 
 
       //Call when error 
 
       }); 
 
      }, 
 
      onError: function(result) { 
 
       sendPayment(result, function() { 
 
       //Call when success 
 
       }, function() { 
 
       //Call when error 
 
       }); 
 
      }, 
 
      onClose: function() { 
 
       sendPayment(result, function() { 
 
       //Call when success 
 
       }, function() { 
 
       //Call when error 
 
       }); 
 
      } 
 

 
      }); 
 
     } 
 
     }); 
 
    }); 
 
    } 
 
});

関連する問題