重构代码以使用jQuery 1.3.2

时间:2013-12-09 12:48:50

标签: javascript jquery jquery-1.3.2

我有一些代码,我正在为客户端编写,他们目前正在使用Drupal 6和jQuery 1.3.2 - 我无法控制他们使用的jQuery,所以我试图编写一些基于的代码假设它不会很快改变。我一直在重新编写代码,现在它的工作时间早在1.4,但我已经碰到了一个砖墙,试图让它在1.3上工作。

我正在使用FlatUI的单选按钮 - 这里的原始按钮(https://raw.github.com/designmodo/Flat-UI/master/js/flatui-radio.js) - 我已经重新使用了它,但找不到阻止它触发的内容。

我已经设置了一个JSFiddle来演示我的问题。除了年龄选择部分之外,一切都工作正常,每个年龄的左边都应该有复选框(13 +,14 +等)。正如我所说,它可以追溯到1.4,甚至可以查看jQuery文档,我找不到任何其他不适用于1.3的内容。

http://jsfiddle.net/dvw9F/

以下是无效的代码:

/* =============================================================
* flatui-radio.js v0.0.3
* ============================================================ */

(function ($) {

'use strict';

/* RADIO PUBLIC CLASS DEFINITION
* ============================== */

var Radio = function (element, options) {
    this.init(element, options);
};

Radio.prototype = {

    constructor: Radio,

    init: function (element, options) {

        var $el = this.$element = $(element);

        this.options = $.extend({}, $.fn.radio.defaults, options);
        $el.before(this.options.template);
        this.setState();

    },

    setState: function () {

        var $el = this.$element,
            $parent = $el.closest('.radio');

        if ($el.attr('disabled') === true) {
            $parent.addClass('disabled');
        }

        if ($el.attr('checked') === true) {
            $parent.addClass('checked');
        }

    },

    toggle: function () {

        var d = 'disabled',
            ch = 'checked',
            $el = this.$element,
            checked = false,
            $parent = $el.closest('.radio'),
            $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
            $elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
            e = $.Event('toggle');

        if ($el.attr(ch) === true) {
            checked = true;
        }

        $elemGroup.not($el).each(function () {

            var $el = $(this),
                $parent = $(this).closest('.radio');

            if ($el.attr(d) !== true) {
                $parent.removeClass(ch) && $el.removeAttr(ch).trigger('change');
            }

        });

        if ($el.attr(d) !== true) {

            if (checked === false) {
                $parent.addClass(ch) && $el.attr(ch, true);
            }

            $el.trigger(e);

            if (true !== $el.attr(ch)) {
                $el.trigger('change');
            }

        }

    },

    setCheck: function (option) {

        var ch = 'checked',
            $el = this.$element,
            $parent = $el.closest('.radio'),
            checkAction = option === 'check' ? true : false,
            checked = false,
            $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
            $elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
            e = $.Event(option);

        if ($el.attr(ch) === true) {
            checked = true;
        }

        $elemGroup.not($el).each(function () {

            var $el = $(this),
                $parent = $(this).closest('.radio');

            $parent.removeClass(ch) && $el.removeAttr(ch);

        });

        $parent[checkAction ? 'addClass' : 'removeClass'](ch) && checkAction ? $el.attr(ch, ch) : $el.removeAttr(ch);
        $el.trigger(e);

        if (checked !== $el.attr(ch)) {
            $el.trigger('change');
        }

    }

};

/* RADIO PLUGIN DEFINITION
* ======================== */

var old = $.fn.radio;

$.fn.radio = function (option) {

    return this.each(function () {

        var $this = $(this),
            data = $this.data('radio'),
            options = $.extend({}, $.fn.radio.defaults, $this.data(), typeof option === 'object' && option);

        if (!data) {
            $this.data('radio', (data = new Radio(this, options)));
        }

        if (option === 'toggle') {
            data.toggle();
        }

        if (option === 'check' || option === 'uncheck') {
            data.setCheck(option);
        } else if (option) {
            data.setState();
        }

    });

};

$.fn.radio.defaults = {
    template: '<span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span>'
};

/* RADIO NO CONFLICT
* ================== */

$.fn.radio.noConflict = function () {

    $.fn.radio = old;
    return this;

};

/* RADIO DATA-API
* =============== */

$('.radio').live('click', '[data-toggle^=radio], .radio', function (e) {

    var $radio = $(e.target);

    if (e.target.tagName !== "A") {

        e && e.preventDefault() && e.stopPropagation();
        if (!$radio.hasClass('radio')) {
            $radio = $radio.closest('.radio');
        }

        $radio.find(':radio').radio('toggle');

    }

});

$(function () {

    $('[data-toggle="radio"]').each(function () {
        var $radio = $(this);
        $radio.radio();
    });

});

}(jQuery));

1 个答案:

答案 0 :(得分:3)

如果您要将现有代码转换为以前的版本,只是因为您的某些插件需要在旧版本上运行。我建议你不要这样做。

您可以在同一页面上使用不同的版本,因此无需更新现有代码:

<!-- load jQuery version 1.9.0 -->
<script src="jquery-1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
var $jQuery1_9 = $.noConflict(true);// Here you must need to pass true
                                    //else it will only free $ variable
                                    //and using jQuery with blow libraries
                                    //cause conflict
</script>

//you must load second library later after initializing
//first instance of version and freeup jQuery keyword
//else jQuery keyword will
//cause conflict it you uplaoded both files.

<!-- load jQuery version 1.10.0 -->
<script src="jquery-1.10.0.js" type="text/javascript"></script>
<script type="text/javascript">
var $jQuery1_10 = $.noConflict(true);
</script>

//so now here $ and jQuery both cannot be used

//using $jQuery1_10 will call version 1.10.0 library code
$jQuery1_10( "div p" ).show();

//using $jQuery1_9 will call version 1.9.0 library code
$jQuery1_9( "div p" ).show();