jQuery plugin changing attributes with $ .extend

5

I created an object in a plugin above .fn . For they are usually created within the objects of which we do the following:

(function( $ ){
    $.fn.tooltip = function(options) {

        var defaults = {
          'corDeFundo' : 'yellow'
        };

        var settings = $.extend( {}, defaults, options );

        return this.each(function() {
            $(this).css({ background: settings.corDeFundo });
        });

    }; 
})( jQuery );

But my object was created above:

(function( $ ){
   var m = {
        "input"  : "outado",
        init:function(options){
            console.log(m.seletor);     
            return m.seletor;
        },//INIT FIM    
    }

   $.fn.input= function(method) {       

        var settings = $.extend( {}, defaults, method);

        console.log(m.seletor);
        return m.init.apply( this, arguments );


    }; 
})( jQuery );

$("div").input({
   input : "red"

})

How do I define the attributes of the object, since to get it I do so inside the function: m.seletor . But how do I change what I have to do?

Correction: Just use it as follows:

 var settings = $.extend( {}, m, method );
            console.log(settings.seletor)

My problem really is : How do I pass this changed object to the INIT() function? Will I have to create another object, or what?

    
asked by anonymous 12.05.2015 / 23:14

1 answer

0

If I understand correctly, you want to use the options inside the plugin.

To illustrate I have changed your code and includes the functionality of adding a new class in the

    (function( $ ){
        $.fn.input = function(options) {
            //aqui são definidos os dados padrão
            var defaults = {
                "input"  : "outado"
            };
            //se foi passado algum item, atualiza o padrão...
            //esta variável fica disponível para todos aqui dentro
            options = $.extend(defaults, options);

            //aqui a função que vc deseja executar
            var init = function(target){
                //neste ponto podes realizar o que precisa com o seletor que foi passado como parametro
                //somente como um exemplo vou adicionar uma nova classe no item
                jQuery(target).addClass(options.input);
            };

            //inicializa...
            init(this);
        };
    })( jQuery );

To test you can

jQuery("#teste").input({input:"nova-classe"});
    
26.01.2017 / 14:32