0

Всем привет! При написании плагина, возникает ошибка, что функция $el.onShow(); не видна в этом методе.

    _build : function(){
         var $this = this;
         $this._initialStyle();
         $el.onShow();
    }

Привожу весь код

(function ($, undefined) {
    var defaults = {
        width: 800,
        onShow: function ($el) {},
        onHide: function ($el) {},
        onUpdate: function ($el) {},
        onBeforeStart: function ($el) {},
        onAfterStart: function ($el) {},
        onLoad: function ($el) {}
    };
    $.fn.simplePopup = function (options) {
        if (this.length == 0){
            return this;
        }
        if (this.length > 1) {
            this.each(function () {
                $(this).simplePopup(options);
            });
            return this;
        }
        var plugin = {},
            settings = $.extend(true, {}, defaults, options),
            settingsTemp = {},
            $el = this;
        plugin.$el = this;
        plugin = {
            _initialStyle: function(){
                var $this = this;
                if (settings.width){
                    $('.popup').css('width', settings.width)
                }
                // settings.onBeforeStart.call(this, $el);
                settings.onAfterStart.call(this, $el);
            },           
            _cons: function(){
                console.log('1212');
            },
            _build : function(){
                var $this = this;
                $this._initialStyle();
                $el.onShow();
            }
        };
        plugin._build();
        $el.onShow = function() {
            $el.click(function(){
                 alert('d');
                 plugin._cons();
             });           
        };
       $el.goTo = function () {
        console.log('g')
       };
    };
}(jQuery));

При чем хочется сделать методы, типа

var test = $('.js-popup').simplePopup()
test.goTo(); 

Подскажите, пожалуйста, как правильно передать ей вызов функции? Спасибо!

Ссылка на код https://jsfiddle.net/wn9437de/2/ (ошибка в консоле)

1
  • Быть может ошибка из-за того, что метод присваивается после вызова? Т.е. на момент вызова у $el этого метода нет, юзай debugger;. Хотя весь плагин как-то... странно сделан.
    – user31688
    1 июн 2015 в 9:17

1 ответ 1

0

Ошибка была в том, что не возвращался this Решилось добавлением return this; в конце плагина

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.