пятница, 17 января 2014 г.

Скелет плагина на jQuery с использованием методов

Скелет плагина на jQuery с использованием методов


(function($){
    var methods = {
        init : function(options) {
            console.log('init');
        },
        show : function( ) {
     
        },
        hide : function( ) {
     
        },
        update : function(content) {
            console.log(content);
        }
    };
    $.myPlug = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error( 'Метод с именем ' +  method + ' не существует для $.chats' );
        }
    };
})(jQuery);
$.myPlug(); // init
$.myPlug('update', 'Привет мир'); // Привет мир
$.myPlug('blalbla') // $.error



или

$.tooltip = {
  update: function() {},
  init: function() {},
  show: function() {} // и т.п.
}

и вызывать как $.tooltip.show()

Комментариев нет:

Отправить комментарий

Постоянные читатели

Популярные сообщения