Bootstrap自定义插件 - Getter方法

时间:2016-08-11 18:43:39

标签: javascript twitter-bootstrap-3

我正在尝试为向导编写一个Bootstrap 3 Custom插件。到目前为止我做得很好,但是我在创建返回当前活动页面的方法时遇到了困难。

我希望做到以下几点:

var activePage = $('#wizard').wizard("getActivePage"); // should return the page number 

我有适配器标准Bootstrap代码中的示例:

自:

if (typeof option == 'string') data[option](args);

要:

if (typeof option == 'string') return data[option](args);

在代码中有一个函数:

getActivePage() {
    return this.currentPage;
}

但是,它返回的是JQuery对象而不是页码。

有没有办法做到这一点,还是我这样做错了?

1 个答案:

答案 0 :(得分:0)

好的,在考虑了这个问题之后,我意识到Plugin函数的默认功能是问题,因为它返回了JQuery链(道歉,以下使用了一些Typescript):

我最初有:

function Plugin(option, args) {

    return this.each(function () {
        var $this = $(this);
        var data = $this.data('xyz.wizard');
        var options = $.extend(option, Wizard.DEFAULTS) as WizardOptions;

        if (!data) {
            data = new Wizard(this, options);
            $this.data('xyz.wizard', data);
        }
        if (typeof option === 'string') return data[option](args);
    });
}

然后在顶部添加了一个新部分:

function Plugin(option, args) {

    // New Part - If option is a string, then call the named method and return the result
    if (typeof option === 'string') {
        var data = $(this).data('xyz.wizard');
        if(data) {
            return data[option](args);
        }
    }

    // Otherwise do the default
    return this.each(function () {
        var $this = $(this);
        var data = $this.data('xyz.wizard');
        var options = $.extend(option, Wizard.DEFAULTS) as WizardOptions;

        if (!data) {
            data = new Wizard(this, options);
            $this.data('xyz.wizard', data);
        }
        // if (typeof option === 'string') return data[option](args);
    });
}

它确实有效,但我不确定它是否是正确的方法。

相关问题