javascript
  • jquery
  • ajax
  • dry
  • 2015-11-16 82 views 0 likes 
    0

    在下面的代碼中:我發送兩個相同的ajax請求,唯一的區別是一行,如何將它包裝到一個函數中以保持我的代碼乾淨?如何讓我的代碼在ajax請求中保持乾燥?

    $('.searchable').multiSelect({ 
        selectableHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='Select reservations ex. \"12\"'>", 
        selectionHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='Remove selected reservations \"'>", 
        afterInit: function(ms){ 
         var that = this, 
          $selectableSearch = that.$selectableUl.prev(), 
          $selectionSearch = that.$selectionUl.prev(), 
          selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable:not(.ms-selected)', 
          selectionSearchString = '#'+that.$container.attr('id')+' .ms-elem-selection.ms-selected'; 
    
         that.qs1 = $selectableSearch.quicksearch(selectableSearchString) 
          .on('keydown', function(e){ 
           if (e.which === 40){ 
            that.$selectableUl.focus(); 
            return false; 
           } 
          }); 
    
         that.qs2 = $selectionSearch.quicksearch(selectionSearchString) 
          .on('keydown', function(e){ 
           if (e.which == 40){ 
            that.$selectionUl.focus(); 
            return false; 
           } 
          }); 
        }, 
        afterSelect: function(value){ 
         $.ajax({ 
          type: 'GET', 
          url: '/police/get_res_price?price=' + value, 
          success: function (data) { 
           var initial_price = parseInt($('.give-me-money').val()); 
           var obj = JSON.parse(data); 
           $.each(obj, function(booking_price, value) { 
            initial_price += parseInt(value.BOOKING_PRICE); 
           }); 
           $('.give-me-money').val(initial_price); //set total 
          } 
         }); 
         this.qs1.cache(); 
         this.qs2.cache(); 
        }, 
        afterDeselect: function(value){ 
         $.ajax({ 
          type: 'GET', 
          url: '/police/get_res_price?price=' + value, 
          success: function (data) { 
           var initial_price = parseInt($('.give-me-money').val()); 
           var obj = JSON.parse(data); 
           $.each(obj, function (booking_price, value) { 
            initial_price -= parseInt(value.BOOKING_PRICE); 
           }); 
           $('.give-me-money').val(initial_price); //set total 
          } 
         }); 
         this.qs1.cache(); 
         this.qs2.cache(); 
        } 
    }); 
    
    +2

    怎麼樣?去做就對了。 'function foo(){ajax stuff here} ... afterDelesect:foo,afterSelect:foo' –

    +0

    它們之間有什麼區別?我不能把他們分開。 – Oleander

    +0

    我不確定我是否明白問題所在;似乎你已經知道答案(「將它們包裝在一個函數中」)。實際上你應該抽象出兩個函數:前/後函數和成功函數。只有成功的功能不同;你可以編寫一個函數發生器,它需要一個arg來指示加/減,或者讓成功函數調用一個小的幫助函數來完成實際的數學運算等。 –

    回答

    2
    var ajaxHandler = function(decrement) { 
        return function(value){ 
         $.ajax({ 
          type: 'GET', 
          url: '/police/get_res_price?price=' + value, 
          success: function (data) { 
           var initial_price = parseInt($('.give-me-money').val()); 
           var obj = JSON.parse(data); 
           $.each(obj, function (booking_price, value) { 
            if (decrement) { 
             initial_price -= parseInt(value.BOOKING_PRICE); 
            } else { 
             initial_price += parseInt(value.BOOKING_PRICE); 
            } 
           }); 
           $('.give-me-money').val(initial_price); //set total 
          } 
         }); 
         this.qs1.cache(); 
         this.qs2.cache(); 
        } 
    } 
    
    $('.searchable').multiSelect({ 
        // other props 
        afterSelect: ajaxHandler(false) 
        afterDeselect: ajaxhander(true) 
    }); 
    
    +0

    請看現在編輯的代碼,完整的一個我不能把它分解成函數,我需要一些東西,我可以在afterselect和beforeselect內部調用 –

    2

    將它們都包含在一個函數中,該函數需要operationType參數。您可以使用該參數在減去時乘以-1。這樣你總是添加你的代碼,但是在取消選擇操作中具有減去的效果。

    $(".searchable").multiSelect({ 
        //selectableHeader etc, 
        afterSelect: function(value) { selectionChange(value, "select"); }, 
        afterDeselect: function(value) { selectionChange(value, "deselect"); } 
    }); 
    
    
    
    
    function selectionChange(value, operationType) { 
        var bookingPrice; 
        if(operationType === "deselect") { 
         bookingPrice = parseInt(value.BOOKING_PRICE) * -1; 
        } else if(operationType === "select") { 
         bookingPrice = parseInt(value.BOOKING_PRICE); 
        } 
    
        $.ajax({ 
         type: 'GET', 
         url: '/police/get_res_price?price=' + value, 
         success: function (data) { 
          var initial_price = parseInt($('.give-me-money').val()); 
          var obj = JSON.parse(data); 
          $.each(obj, function (booking_price, value) { 
           initial_price += bookingPrice; 
          }); 
          $('.give-me-money').val(initial_price); //set total 
         } 
        }); 
        this.qs1.cache(); 
        this.qs2.cache(); 
    } 
    
    +0

    我認爲這是更好的答案,因爲它們共享相同的內部函數參考。 – SirTophamHatt

    +0

    您需要將'bookingPrice'部分移動到'$ .each()'中才能正常工作。 –

    +0

    請現在看到編輯後的代碼,完整的一個我不能分解成函數我需要的東西,我可以打電話在afterselect和之前選擇 –

    相關問題