Alexander Nazarov (nfokz) wrote in changelog,
Alexander Nazarov
nfokz
changelog

[ljcom] r12394: LJSUP-13138: Extend basicWidget pub/sub ...

Committer: anazarov
LJSUP-13138: Extend basicWidget pub/sub system with _one method
U   trunk/htdocs/js/jquery/jquery.lj.basicWidget.js
Modified: trunk/htdocs/js/jquery/jquery.lj.basicWidget.js
===================================================================
--- trunk/htdocs/js/jquery/jquery.lj.basicWidget.js	2012-08-01 13:10:49 UTC (rev 12393)
+++ trunk/htdocs/js/jquery/jquery.lj.basicWidget.js	2012-08-01 13:46:51 UTC (rev 12394)
@@ -103,11 +103,23 @@
 
 			__callbacks[ type ].push( {
 				fn: callback,
-				owner: this
+				owner: this,
+				once: false
 			} );
 		},
 
 		/**
+		 * Subscribe to one selected event with the callback.
+		 *
+		 * @param {String} type Event type.
+		 * @param {Function} callback Function that should be fired on the event.
+		 */
+		_one: function( type, callback ) {
+			this._on( type, callback );
+			__callbacks[ type ][ __callbacks[ type ].length - 1 ].once = true;
+		}, 
+
+		/**
 		 * Remove subscription on the event.
 		 *
 		 * @param {String} type Event type.
@@ -147,11 +159,14 @@
 				}
 
 				//we delete supressed event flag only after firing event because 
-				//wedget can subscribe more than one callback
+				//widget can subscribe more than one callback
 				while(cbs[ --i ]) {
 					if( type in cbs[ i ].owner.__suppressedEvents ) {
 						delete cbs[ i ].owner.__suppressedEvents[ type ];
 					}
+
+					//delete callbacks that should be fired only once
+					if( cbs[ i ].once ) { cbs.splice(i, 1); }
 				}
 			}
 		},

Tags: anazarov, js, ljcom, nfokz
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments