2009-11-30 70 views
0

我工作在一個jQuery插件,我有一個錯誤,我想修改插件的一個實例的選項,但是當我嘗試它時,我更改所有實例的選項。好吧,我的英語很糟糕,所以比我放我的代碼更好。是一種通用語言......如何設置jquery插件一次?

(函數($){

$.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); 

請,任何一個能幫助我。

THX

回答

0

好,只是有asnwer的問題......我在我的代碼中的錯誤:

的代碼行,是錯誤的

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

正確的線路包括一個空字典:

this.config = $ .extend(真,{},$ .fn.interruptor.defaults,選項);

所以我需要初始化結構。好吧,這是一個愚蠢的錯誤,我很抱歉。好吧,那樣的話,現在一個可以設置的方法是讓我的插件的選項一次性獲得。