]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Event handler reference storage; avoids duplicate event calls; implemented unlisten()
authorrwldrn <waldron.rick@gmail.com>
Sat, 4 Dec 2010 17:10:56 +0000 (12:10 -0500)
committerrwldrn <waldron.rick@gmail.com>
Sat, 4 Dec 2010 17:10:56 +0000 (12:10 -0500)
popcorn-api-draft.js
test/popcorn-api-draft-unit.js

index fd1ea207891db4dee8ff23a83a917a1740fbdc2c..20d612fdd23b1db3380e8d9ad58f94fe9312193c 100644 (file)
       }, 
       listen: function ( type, fn ) {
         
-        var self = this;
+        var self = this, hasEvents = true;
         
         if ( !this.data.events[type] ) {
           this.data.events[type] = {};
+          hasEvents = false;
         }
         
         //  Register 
         this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn;
         
-        if ( Popcorn.events.all.indexOf( type ) > -1 ) {
-          
+        // only attach one event of any type          
+        if (!hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
+
           this.video.addEventListener( type, function( event ) {
             
             Popcorn.forEach( self.data.events[type], function ( obj, key ) {
               obj.call(self, event);
-            });            
+            });
+            
+            //fn.call( self, event );
           
-          }, false);
-        
+          }, false);          
         }
         return this;
       }, 
+      unlisten: function( type ) {
+        this.data.events[type] = null;
+        return this;        
+      },      
       special: {
         // handles timeline controllers
         play: function () {
   };
   
   //  Extend listen and trigger to all Popcorn instances
-  Popcorn.forEach( ["trigger", "listen"], function ( key ) {
+  Popcorn.forEach( ["trigger", "listen", "unlisten"], function ( key ) {
     Popcorn.p[key] = Popcorn.events.fn[key];
   });  
   
index e18d2f04fc1e4252153e378d04d49b0ae5206522..9d92f0cde730e4b1fed30ec0f00e4fb11d40ffd1 100644 (file)
@@ -118,19 +118,83 @@ test("Popcorn Plugin", function () {
   
 });
 
+test("Popcorn Events Stored By Type", function () {
+  
+  expect(6)
+  
+  var p = Popcorn("#video"), 
+      count = 0,
+      fired = 0
+      ;
 
+  function plus(){ 
+    if ( ++count == 4 ) {
+      
+      if ( fired === 4 ) {
+        ok( true, fired + " callbacks fired from 1 handler" );
+      }
+      else {
+        ok( false, fired + " callbacks fired - CHECK HANDLERS" );
+      }
 
 
-test("Popcorn Events Simulated", function () {
+      p.unlisten("play");
+  
+  
+      ok( !p.data.events["play"], "play handlers removed" );
+  
+        
+      start();
+    } 
+  }
+
+  stop();  
+  
+  
+  p.listen("play", function () {
+    fired++;
+    
+    ok(true, "Play fired");
+    plus();
+  });
+      
+  p.listen("play", function () {
+    fired++;
+
+    ok(true, "Play fired");    
+    plus();
+  });
+
+  p.listen("play", function () {
+    fired++;
+
+    ok(true, "Play fired");    
+    plus();
+  });
 
+  p.listen("play", function () {
+    fired++;
+
+    ok(true, "Play fired");
+    plus();
+  });
+  
+  p.trigger("play");
+  
+
+  
+});
+
+
+test("Popcorn Events Simulated", function () {
 
   var p = Popcorn("#video"),
       completed = [], 
       eventtest = "loadstart progress suspend emptied stalled play pause " + 
-                        "loadedmetadata loadeddata waiting playing canplay canplaythrough " + 
-                        "seeking seeked timeupdate ended ratechange durationchange volumechange", 
+                  "loadedmetadata loadeddata waiting playing canplay canplaythrough " + 
+                  "seeking seeked timeupdate ended ratechange durationchange volumechange", 
       events = eventtest.split(/\s+/g);                              
-  
+
   
   var expects = events.length, 
       count = 0;