tags:

views:

57

answers:

1

Hi, I´m working in a jQuery pluging, and i have one error, i want modify the option of one instance of the plugin, but when i try it, i change the options for the all instances. Ok, my english is horrible, so is better than i put my code. Is a universal language ...

(function($){

$.fn.interruptor = function(options){
 // Variable iniciales
 var esMetodo = (typeof options == 'string'),
   args = Array.prototype.slice.call(arguments, 1),
   returnValue = this;

 // Previene la llamada a los métodos internos  
 if(esMetodo && options.substring(0, 1) == '_') return returnValue;

 (esMetodo)
  ? this.each(function(){
   var instance = $.data(this, 'interruptor');
   console.dir(instance);
   return ($.isFunction(instance[options])) ? instance[options].apply(instance, args) : returnValue;
  })
  : this.each(function(){
   ($.data(this, 'interruptor') 
    || $.data(this, 'interruptor', new $.interruptor(this, options))._init());
  });

  return returnValue;
} // fin $.fn.interruptor

$.interruptor = function(elem, options){
 this.config = $.extend(true, $.fn.interruptor.defaults, {numero: parseInt(Math.random()*100)}, options);
 this.id = $(elem).attr('id');
};

$.interruptor.prototype = {
 _init: function(){
  console.info(this.config.numero);
 },
 setter: function(k, v){
  this.config[k] = v;
  return false;
 },
 getter: function(){
  return this.id;
 },
 debug: function(msg){
  console.info(msg);
  console.dir(this.config);
 }
};


//Definición de los valores por defecto.
$.fn.interruptor.defaults = {
  numero: 0,
  img:    'images/iphone_switch_square2.png',  // Dirección base en la que se encuentra la imagen que genera el interruptor
  estado:  true,                 // 0 => OFF, 1 => ON 
  deshabilitado: false,              // Indica si el plugin se encuentra actualmente deshabilitado
  duracion: 200,                 // Duración en milisegundos del cambio de estado
  funcionOn : function(){alert ('On');},     // Definimos la función que se ejecuta en el On
  funcionOff : function(){alert ('Off');}     // Definimos la función que se ejecuta en el Off
};})(jQuery);

Please, any one can help me.

Thx

A: 

Ok, just have the asnwer to the question ... I have a error in my code:

that code line, is wrong

this.config = $.extend(true, $.fn.interruptor.defaults, options);

the correct line include a empty dictionary:

this.config = $.extend(true, {}, $.fn.interruptor.defaults, options);

So i need init the struct. Ok that is a dummdy error, i am sorry.

Ok, that way, now a can setter y getter the options of my plugin once instance.

Carlosvillu