Variable privée dans un plugin jQuery

Résolu/Fermé
monkey_monk Messages postés 641 Date d'inscription mercredi 1 juillet 2009 Statut Membre Dernière intervention 10 août 2012 - 4 août 2010 à 02:00
monkey_monk Messages postés 641 Date d'inscription mercredi 1 juillet 2009 Statut Membre Dernière intervention 10 août 2012 - 4 août 2010 à 23:45
Bonjour,

je planche depuis quelques heures sur une question de variable privée à l'intérieur d'un plugin jQuery.

Avant d'aller plus loin, voici mon code test :
/* PLUGINNAME */
(function($){
	$.fn.test = function(options){
		var opts = $.extend({},$.fn.test.defaults,options);
		
		return this.each(function(){
			init($(this));
			opts.callback();
		});

		function init(obj){
			var ref = obj;
			
			var o = $.meta ? $.extend({}, opts, obj.data()) : opts;
			o.param = obj.text();
			console.log(o.param+' : '+ref.text());
			// me renvoi bien "instance 1" puis "instance 2"
			
			obj.append('<a class="testeur" href="#" title="click test">click test</a>');
			obj.children('.testeur').each(function(){
				$(this).click(function(e){
					e.preventDefault();
					testeur(ref,o);
				});
			});
		}
		function testeur(obj,o){
			alert(obj.text());
			alert(o.param);	// renvoi systématiquement "instance 2" (???)
		}
	};
	$.fn.test.defaults = {
		param : '',
		callback : function(){}
	};
})(jQuery);
/* /PLUGINNAME */
$(function(){
	$('.test').test();
});

et la structure HTML :
<div id="instance1" class="test">
	instance 1
</div>
<div id="instance2" class="test">
	instance 2
</div>


Le problème est qu'au moment du second alert, je ne reçoit que "instance 2".
...alors que je veux désespérément avoir "instance 1" pour le premier et "instance 2" pour le second.

Quelqu'un pourrait éclairer ma lanterne ?

Un grand merci d'avance parce que je sèche vraiment là... ^^
A voir également:

2 réponses

monkey_monk Messages postés 641 Date d'inscription mercredi 1 juillet 2009 Statut Membre Dernière intervention 10 août 2012 128
4 août 2010 à 23:27
up

Je cherche du coté de .data() mais ça cafouille...
0
monkey_monk Messages postés 641 Date d'inscription mercredi 1 juillet 2009 Statut Membre Dernière intervention 10 août 2012 128
4 août 2010 à 23:45
Bon et bien après 10 minutes à lire la doc sur .data() ...je crois bien que j'ai ma solution ! :)

Comme quoi, il faut pas chercher midi à 14h ! ^^

Donc le code final et fonctionnel :
/* PLUGINNAME */
(function($){
	$.fn.pluginName = function(options){
		var opts = $.extend({},$.fn.pluginName.defaults,options);
		
		return this.each(function(){
			$(this).data(opts);
			init($(this));
			opts.callback();
		});

		function init(obj){
			obj.data(opts).param = obj.text();
			
			obj.append('<span class="testeur">click test</span>');
			obj.children('.testeur').click(function(e){
				e.preventDefault();
				testeur(obj);
			});
		}
		function testeur(obj,o){
			alert(obj.text());
			alert(obj.data(opts).param);
		}
	};
	$.fn.pluginName.defaults = {
		param : '',
		param2 : '',
		callback : function(){}
	};
})(jQuery);
/* /PLUGINNAME */
$(function(){
	$('.test').pluginName();
});


Merci quand même ! :D
0