2009-11-30 2 views
0

jQuery pluging에서 작업 중이며 하나의 오류가 있습니다. 플러그인의 한 인스턴스 옵션을 수정하고 싶습니다.하지만 시도 할 때 모든 인스턴스에 대한 옵션이 변경됩니다. 좋아, 내 영어는 끔찍한데, 내 코드를 쓰는 것보다 낫다. 보편적 인 언어입니다 ...jquery plugin을 인스턴스로 설정하는 방법은 무엇입니까?

(함수 ($) {

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

, 하나가 나를 도울 수 바랍니다.

들으

답변

0

좋아, 그냥 질문에 asnwer이 ... 나는 내 코드에서 오류가 있습니다,

그 코드 라인은 잘못

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

올바른 라인은 빈 사전이 포함

this.config = $ .extend (사실, {}, $ .fn.interruptor.defaults, 옵션);

그래서 구조체를 초기화해야합니다. 좋아, 그건 녀석 오류, 미안 해요.

좋아, 그런 식으로, 이제는 한 번 인스턴스 내 플러그인의 옵션을 얻을 수 있습니다.

관련 문제