如何在ajax请求中保持代码干燥?

时间:2015-11-16 21:20:15

标签: javascript jquery ajax dry

如以下代码所示:我发送两个ajax requets,它们是唯一的区别是一行,如何将其包装成一个函数以保持我的代码DRY?

$('.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 个答案:

答案 0 :(得分:2)

将它们都包含在一个带有table forum_index doesn't exists 参数的函数中。在减法时,可以使用该参数乘以-1。这样你总是可以添加你的代码但是具有减去取消选择操作的效果。

operationType

答案 1 :(得分: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)
});