Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于非函数对象的jQuery小部件桥接器_Jquery_Jquery Ui_Jquery Widgets - Fatal编程技术网

基于非函数对象的jQuery小部件桥接器

基于非函数对象的jQuery小部件桥接器,jquery,jquery-ui,jquery-widgets,Jquery,Jquery Ui,Jquery Widgets,作为从jQuery1.6.4升级到1.9.x的一部分,我试图了解如何使用jQueryWidgetBridge来允许访问作为独立对象创建的小部件的功能,而不是作为jQuery的扩展 所有的示例()都使用原型函数作为对象的源定义,它似乎不适合直接从基于{}的定义中定义对象 例如,我定义的小部件如下: var ControlPanel = { instance: null, options: { //Some options here }, sim

作为从jQuery1.6.4升级到1.9.x的一部分,我试图了解如何使用jQueryWidgetBridge来允许访问作为独立对象创建的小部件的功能,而不是作为jQuery的扩展

所有的示例()都使用原型函数作为对象的源定义,它似乎不适合直接从基于{}的定义中定义对象

例如,我定义的小部件如下:

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
newControlPanel.controlPanel("addControl", smartyControlPanel);
然后通过执行以下操作创建其实例:

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

var newControlPanel = $('#controlPanel').controlPanel(controlPanelParams);
newControlPanel.simpleFunction();
//and 
$(newControlPanel).simpleFunction();
现在,我希望能够在创建的对象上调用“simpleFunction”方法,我已尝试通过执行以下操作来执行此操作:

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

var newControlPanel = $('#controlPanel').controlPanel(controlPanelParams);
newControlPanel.simpleFunction();
//and 
$(newControlPanel).simpleFunction();
这两个函数都给出了一个“对象没有名为simpleFunction的方法”错误


那么,我做错了什么?您打算如何在非基于函数的对象上使用$.widget.bridge?

我想我已经明白了。与此相反:

$(newControlPanel).addControl(smartyControlPanel);
通过将函数作为如下参数调用来访问该函数:

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
newControlPanel.controlPanel("addControl", smartyControlPanel);
我还将我的代码转换为基于第一个原型的函数:

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

    this._init();
};

ControlPanel.prototype = {
    //Everything else in here
}
尽管必须将其作为参数调用要比直接将其作为函数调用简单得多