Retour

Utilisez les événements !

Exemple $.proxy()

function SlideShow(base){
	var self = this;
	
	$('.leftControl', base).bind('click', $.proxy(self.onLeftControlClick, self));
	//$('.rightControl', base).bind('click', $.proxy(self.onRightControlClick, self));
	
	/*
	 * Sans $.proxy() on obtient l'erreur "this.manageControls is not a function"
	 * car "onRightControlClick this : [object HTMLButtonElement]"
	 */
	//$('.rightControl', base).bind('click', self.onRightControlClick);
	
	/*
	 * ancienne méthode apply ou call
	 * il est possible d'ajouter des arguments
	 */
	$('.rightControl', base).bind('click', function(e){
		var n = 10;
		
		self.onRightControlClick.apply(self, [e, n]);
		//self.onRightControlClick.call(self, e, n);
	});
	
	this.manageControls();
}
 
SlideShow.prototype = {
	manageControls: function(){
		alert("manageControls OK");
	},
	animate: function(){
		alert("animate OK");
	},
	onLeftControlClick: function(){
		alert("onLeftControlClick this : " + this);
		
		this.manageControls();
		this.animate();
	},
	onRightControlClick: function(){
		var tab = $.makeArray(arguments);
		
		alert("onRightControlClick this : " + this + "\nArguments = " + tab);
		
		this.manageControls();
		this.animate();
	}
};

$(function(){
	new SlideShow("#affiche");
});