如何使用插件引用调用jQuery widget公共方法

时间:2014-04-25 06:33:20

标签: javascript jquery widget

我创建了一个插件弹出窗口并尝试创建引用并访问插件的公共方法,但是它出现错误'未定义的函数'。

 var pop = $('.divCls').popup();
   pop.setTitle('Sample Text');//This is line giving error

这是我的插件

(function ($) {
    $.widget('bs.popup', {
        options:{
//            containment: '.bs-editor-body',
            hideOnClick: '.bs-editor-body',
            showTimeDelay:0
        },
        _create: function (){
            var me = this,
                el = me.element,
                opts = me.options;

            el.addClass('bs-popup');
            el.html($('<div class="bs-popup-body">' + el.html() + '</div>'));
            el.body = el.find('.bs-popup-body');

           me._refresh();
           if(opts.draggable){
                el.draggable({containment:'document',cancel:'.bs-popup-body'});
                el.unbind().bind('click',function(event){
                    event.stopPropagation();
                });
            }
           $(document).on('click','body',function(){
               me.hide();
           });
            me.show();
        },
        _init:function(){
            this.show();
            this._refresh();
        },
        _refresh:function(){
            var me = this,
                el = me.element,
                opts = me.options;
            el.find('.bs-popup-header').remove();
            if(opts.title && opts.title !='') {
                el.prepend('<div class="bs-popup-header"></div>');
                el.hdr = el.find('.bs-popup-header');
                me.setTitle(opts.title);
                el.body.css({'border-top-left-radius':0,'border-top-right-radius':0})
            } else {
                el.body.css({'border-radius': el.body.css('border-bottom-left-radius')})
            }
            if(opts.width)me.setWidth(opts.width);
            if(opts.height)me.setHeight(opts.height);
            if(opts.left && opts.top) me.setXY(opts.left ,opts.top);
            if(opts.cls) el.addClass(opts.cls);
        },
        show: function () {
            debugger;
            this.hide();
            this.element.show(this.options.showTimeDelay);
        },
        hide: function () {
            this.element.hide();
            this._destroy();
        },
        setTitle:function(title){
            this.getHeader().text(title);
        },
        setXY: function (x,y){
            var me = this,
                el = me.getElement(),
                opts = me.options;
            if (x && y) {
                el.css({'left': x, top: y});
            } else {
//                var confine = evtRef.closest(opts.containment);
            }
        },
        setWidth: function (value){
            this.getBody().css({width: value});
        },
        setHeight: function (value) {
            this.getBody().css({height: value});
        },
        getElement:function(){
            return this.element;
        },
        getHeader:function(){
           return this.element.hdr;
        },
        getBody:function(){
            return this.element.body;
        },
        _destroy:function(){
//          $.Widget.prototype.destroy.call(this);
        }
    });
}(jQuery));

1 个答案:

答案 0 :(得分:2)

你可以尝试

pop.popup('setTitle', 'Sample Text');

Widget Method Invocation

  

要使用窗口小部件的插件调用方法,请传递名称   方法作为字符串。

Widget Public Methods