]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#390] Re-org; Adds instance return; Adds Popcorn.removeInstanceById(); Adds unit...
authorrwldrn <waldron.rick@gmail.com>
Tue, 8 Mar 2011 19:28:13 +0000 (14:28 -0500)
committerrwldrn <waldron.rick@gmail.com>
Tue, 8 Mar 2011 19:28:13 +0000 (14:28 -0500)
popcorn.js
test/popcorn.unit.js

index 91d0bc07be49fe769c1712411edea4e429d8bf96..25510c578e9a041644f4d285da46109445dc187e 100644 (file)
     //  Return new Popcorn object
     return new Popcorn.p.init( entity );
   };
-  
+
+  //  Instance caching
   Popcorn.instances = [];
-  
   Popcorn.instanceIds = {};
-  
-  Popcorn.id = null;
-  
-  Popcorn.removeInstance = function( popcornInstance ) {
+
+  Popcorn.removeInstance = function( instance ) {
     //  If called prior to any instances being created
     //  Return early to avoid splicing on nothing
     if ( !Popcorn.instances.length ) {
-      
       return;
-      
     }
-  
-    Popcorn.instances.splice( Popcorn.instanceIds[ popcornInstance.id ], 1 );
 
-    delete Popcorn.instanceIds[ popcornInstance.id ];
-    
+    //  Remove instance from Popcorn.instances 
+    Popcorn.instances.splice( Popcorn.instanceIds[ instance.id ], 1 );
+
+    //  Delete the instance id key
+    delete Popcorn.instanceIds[ instance.id ];
+
+    //  Return current modified instances
+    return Popcorn.instances;
   };
 
   //  Addes a Popcorn instance to the Popcorn instance array
-  Popcorn.addInstance = function( popcornInstance ) {
-  
-    if ( !popcornInstance.video.id ) { 
-    
-      popcornInstance.id = "__popcorn" + Popcorn.instances.length;
-    
-    }
-    
-    else {
-    
-      popcornInstance.id = popcornInstance.video.id;
-    
-    }
-    
-    Popcorn.instanceIds[ popcornInstance.id ] = Popcorn.instances.length;
-    
-    Popcorn.instances.push( popcornInstance );
-    
+  Popcorn.addInstance = function( instance ) {
+
+    var instanceLen = Popcorn.instances.length,
+        instanceId = instance.video.id && instance.video.id;
+
+    //  If the video element has its own `id` use it, otherwise provide one
+    //  Ensure that instances have unique ids and unique entries
+    //  Uses `in` operator to avoid false positives on 0
+    instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId || 
+                      "__popcorn" + instanceLen;
+
+    //  Create a reference entry for this instance
+    Popcorn.instanceIds[ instance.id ] = instanceLen;
+
+    //  Add this instance to the cache
+    Popcorn.instances.push( instance );
+
+    //  Return the current modified instances
+    return Popcorn.instances;
   };
 
-  //  User passes in the name of the Popcorn instance and receive a popcorn object
-  Popcorn.getInstanceById = function( name ) {
-  
-    return Popcorn.instances[ Popcorn.instanceIds[ name ] ];
-    
+  //  Request Popcorn object instance by id
+  Popcorn.getInstanceById = function( id ) {
+    return Popcorn.instances[ Popcorn.instanceIds[ id ] ];
   };
-  
+
+  //  Remove Popcorn object instance by id
+  Popcorn.removeInstanceById = function( id ) {
+    return Popcorn.removeInstance( Popcorn.instances[ Popcorn.instanceIds[ id ] ] );
+  };
+
   //  Declare a shortcut (Popcorn.p) to and a definition of
   //  the new prototype for our Popcorn constructor
   Popcorn.p = Popcorn.prototype = {
index b28dbf536a4441eeabc6ad8df5194f5b85b4e33a..aefb3c808033719c82619fc8424e4fbfb8067bbf 100755 (executable)
@@ -68,7 +68,7 @@ test("Utility", function () {
 });
 
 test("Instances", function() {
-  var expects = 9, 
+  var expects = 10, 
       count   = 0,
       instance;
   
@@ -110,7 +110,20 @@ test("Instances", function() {
   
   equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" );
   plus();
-   
+
+
+  // Instance handling
+
+  //  Create another instance
+  Popcorn("#video");
+
+  //  Get a reference to remove
+  var remove = Popcorn.instances[1];
+
+  //  Remove and check the length of the currently cached instances
+  equal( Popcorn.removeInstanceById( remove.id ).length, 1, "Removing an instance by id: 1 instance remains" );
+  plus();
+
 });
 
 test("guid", function () {