Add methods to plugin namespace without selector

2

I'm creating a plugin in js and need to add some methods to this plugin.

This is the base of the plugin so far:

    (function ($) {
    function jarbas(params) {
        ...
    }

    $.fn.jarbas = function (params) {
        // cria funcionalidade a partir do método sem seletor $.jarbas(params);
    }

    // defaults
    $.fn.jarbas.defaults = { ... };

    $.extend({
        jarbas : function (params) {
            switch (typeof params) {
                case 'string':

                    break;
                case 'object':
                    return new jarbas(params);
                    break;
            }
        }
    });
})(jQuery);

In the use of the plugin I do (works perfect):

$.jarbas({ ... });
$('seletor').jarbas({ ... });

Is it possible to create methods extending the plugin name without the need for selector? for example:

$.jarbas.remapAll();
    
asked by anonymous 22.12.2014 / 14:32

2 answers

2

Yes it is possible.

Just like $.fn.jarbas.defaults = { ... }; to define a property of this function can add a new method by passing a function:

$.fn.jarbas.remapAll= function(){
    // fazer o que quiser;
}

Example:

$.fn.jarbas = function (params) {
    // cria funcionalidade a partir do método sem seletor $.jarbas(params);
}

// defaults
$.fn.jarbas.defaults = {
    foo: 'bar'
};

$.fn.jarbas.remapAll = function () {
    alert('Eureka! Achei o ' + this.defaults.foo + '!');
}

// testar

alert($.fn.jarbas.defaults.foo);
$.fn.jarbas.remapAll ();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
22.12.2014 / 14:40
2

Your plugin has a chaining problem of jQuery objects. What the documentation recommends is for your plugin to always return this (if it always applies to collections of only 1 object), or this.each(...) , if the plugin needs to handle more than one selected object.

This allows you to chain jQuery methods into the same selection, for example:

$('a').jarbas().show();

The way your plugin is built, by returning new jarbas , this will not be possible. You also can not call $.jarbas(); if you set the method to $.fn . You can, of course, call $.fn.jarbas() .

As for how to make the plugin work as namespace , Sergio has answered and I will not repeat what he said. Because JavaScript functions are objects, you can hang any properties you want on them. But this is kind of strange in the jQuery world. Plugins generally depend on selectors, and are not used with namespaces that way. Which brings me to your final question:

  

Is it possible to create methods by extending the plugin name without the need for a selector?

Yes, but then it does not make sense to put your methods in $.fn . What's in there is used as a prototype of jQuery objects, which are selections of DOM nodes. If you want something that is independent of the selector, set it directly to the object jQuery (or $ ):

(function($) {
    $.jarbas = function(){
        document.body.innerHTML += "jarbas<br>";
    };
    $.jarbas.remapAll = function() {
        document.body.innerHTML += "jarbas remapped<br>";
    }
}(jQuery));

$.jarbas();
$.jarbas.remapAll();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
23.12.2014 / 04:35