Return to Snippet

Revision: 41737
at February 23, 2011 23:16 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var context = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		//_events.bind(eventType, handler);			// older jQuery versions
		_events.bind(eventType, $.proxy(handler, context));	// jQuery 1.4 and newer
		return this;
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		//_events.unbind(eventType, handler);			// older jQuery versions
		_events.unbind(eventType, $.proxy(handler, context));	// jQuery 1.4 and newer
		return this;
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters		String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		_events.trigger(eventType, extraParameters);
		return this;
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41736
at February 23, 2011 23:15 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		//_events.bind(eventType, handler);			// older jQuery versions
		_events.bind(eventType, $.proxy(handler, context));	// jQuery 1.4 and newer
		return this;
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		//_events.unbind(eventType, handler);			// older jQuery versions
		_events.unbind(eventType, $.proxy(handler, context));	// jQuery 1.4 and newer
		return this;
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters		String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		_events.trigger(eventType, extraParameters);
		return this;
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41735
at February 23, 2011 01:45 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		return _events.bind(eventType, handler);
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		return _events.unbind(eventType, handler);
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters		String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		return _events.trigger(eventType, extraParameters);
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41734
at February 23, 2011 00:58 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		return _events.bind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.bind(eventType, handler);			//older jQuery versions
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		return _events.unbind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.unbind(eventType, handler);			//older jQuery versions
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters		String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		return _events.trigger(eventType, extraParameters);
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41733
at February 23, 2011 00:58 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		return _events.bind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.bind(eventType, handler);			//older jQuery versions
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		return _events.unbind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.unbind(eventType, handler);			//older jQuery versions
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType			String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters		String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		return _events.trigger(eventType, extraParameters);
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41732
at February 23, 2011 00:57 by wildpeaks


Updated Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		return _events.bind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.bind(eventType, handler);				//older jQuery versions
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		return _events.unbind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.unbind(eventType, handler);			//older jQuery versions
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType			String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters	String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		return _events.trigger(eventType, extraParameters);
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Revision: 41731
at February 23, 2011 00:56 by wildpeaks


Initial Code
/************************
 * Class definition
 ************************/
var MyObjectClass = function(parameters){

	var $ = jQuery;
	var _this = this;


	/**
	 * Detached element used to tap into the regular jQuery events model.
	 * @private
	 */
	var _events = $(document.createElement('span'));

	/**
	 * Attaches a handler to an event for the class instance.
	 * @param		eventType		String		A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
	 * @param		handler			Function	A function to execute each time the event is triggered.
	 * @see http://api.jquery.com/bind/
	 */
	this.bind = function( eventType, handler ){
		return _events.bind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.bind(eventType, handler);					//older jQuery versions
	};

	/**
	 * Removes a previously-attached event handler from the class instance.
	 * @param		eventType		String		A string containing a JavaScript event type, such as click or submit.
	 * @param		handler			Function	The function that is to be no longer executed.
	 * @see http://api.jquery.com/unbind/
	 */
	this.unbind = function( eventType, handler ){
		return _events.unbind(eventType, $.proxy(handler, _this));	//jQuery 1.4 and newer
		//return _events.unbind(eventType, handler);				//older jQuery versions
	};

	/**
	 * Executes all handlers and behaviors attached to the class instance for the given event type.
	 * @param		eventType_			String		A string containing a JavaScript event type, such as click or submit.
	 * @param		extraParameters_	String		An array of additional parameters to pass along to the event handler.
	 * @see http://api.jquery.com/trigger/
	 */
	this.trigger = function( eventType, extraParameters ){
		return _events.trigger(eventType, extraParameters);
	}

	//
	//...
	//
	
	/**
	 * Example public method to make the object send an event of type <code>my_event</code>.
	 */
	this.test = function(){
		_events.trigger("my_event");
	};

};



/************************
 * Usage example
 ************************/

// Creates an instance of the class
var obj = new MyObjectClass();

// Listens to the event the same way you would with DOM elements
obj.bind(
	"my_event",
	function(e){
		console.log("The custom event has been received");
	}
);

// Makes the object fire an event
obj.test();

Initial URL
http:/www.wildpeaks.com

Initial Description
**Important: this snipplet has moved to Github.**

 - [jQuery events from non-DOM objects (supports chaining)](https://gist.github.com/1973168)

Initial Title
jQuery events from non-DOM objects (supports chaining)

Initial Tags


Initial Language
jQuery