2016-08-11 59 views
0

我正在嘗試爲嚮導編寫Bootstrap 3自定義插件。到目前爲止,我確實沒問題,但是我很難創建返回當前活動頁面的方法。Bootstrap自定義插件 - Getter方法

我希望做到以下幾點:

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

我適配器在標準的引導代碼中發現的例子:

來源:

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

要:

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

An d有以下代碼中的功能:

getActivePage() { 
    return this.currentPage; 
} 

但是,它返回的是JQuery對象而不是頁碼。

有沒有辦法做到這一點,或者我對這一切都有錯?

+0

給出一些詳細的代碼是當前頁面變量和getactivepage定義 –

回答

0

好了,想着這對於多一點的時間後,我意識到,插件功能的默認功能是它返回JQuery的鏈條問題(道歉,下面通過一些打字稿):

我原本有:

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); 
    }); 
} 

它似乎工作,但我不知道這是否是CORRE ct方法。