2013-02-05 31 views
0

作爲從jQuery 1.6.4升級到1.9.x的一部分,我試圖理解如何使用jQuery小部件橋來允許訪問作爲獨立創建的小部件的函數對象,而不是作爲jQuery的擴展。在基於非函數的對象上的jQuery小部件橋

所有示例(like this one)都使用原型函數作爲對象的源定義,該對象的源定義似乎沒有映射爲直接從基於定義的對象定義對象。

比如我定義窗口小部件是這樣的:

var ControlPanel = { 

    instance: null, 

    options: { 
     //Some options here 
    }, 

    simpleFunction: function(){ 
     alert("I am a function"); 
    }, 

    _init: function() { 
     //Some init stuff here 
    } 
} 

$.widget("basereality.controlPanel", ControlPanel); // create the widget 

//This doesn't appear to work... 
$.widget.bridge('controlPanel', $.basereality.controlPanel); //'Bridge' the widget 

然後做創建它的一個實例:

var controlPanelParams = { 
    // some options go here 
}; 

var newControlPanel = $('#controlPanel').controlPanel(controlPanelParams); 

我現在想能夠調用的方法「 simpleFunction',我試着去做:

newControlPanel.simpleFunction(); 
//and 
$(newControlPanel).simpleFunction(); 

兩者都是g一個'對象沒有叫做simpleFunction'的方法錯誤。

那麼我在做什麼錯了,你打算如何在基於非函數的對象上使用$ .widget.bridge?

回答

0

我想我已經想通了。取而代之的是:

$(newControlPanel).addControl(smartyControlPanel); 

訪問通過調用它作爲像這樣的參數功能:

newControlPanel.controlPanel("addControl", smartyControlPanel); 

而且我也將我的代碼已經被基於第一個原型的函數:

var ControlPanel = function(options, element){ 
    this.options = options; 
    this.element = element; 

    this._init(); 
}; 

ControlPanel.prototype = { 
    //Everything else in here 
} 

雖然不得不將它作爲參數調用,但它比直接調用它作爲函數更容易理解。

相關問題