Popcorn.instances = [];
Popcorn.instanceIds = {};
+
Popcorn.removeInstance = function(id) {
- Popcorn.instances.splice(Popcorn.instanceIds[id], 1);
- delete Popcorn.instanceIds[id];
+ // 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[ id ], 1 );
+
+ delete Popcorn.instanceIds[ id ];
+
};
+ //addes a Popcorn instance to the Popcorn instance array
Popcorn.addInstance = function(popcornInstance) {
- if (popcornInstance.video.id === "undefined" || !popcornInstance.video.id.length) {
+ if (!popcornInstance.video.id ) {
+
popcornInstance.video.id = "__popcorn" + Popcorn.instances.length;
+
}
+
Popcorn.instanceIds[popcornInstance.video.id] = Popcorn.instances.length;
+
Popcorn.instances.push(popcornInstance);
+
};
+ //User passes in the name of the Popcorn instance and receive a popcorn object
Popcorn.getInstanceById = function(name) {
return Popcorn.instances[Popcorn.instanceIds[name]];
};
});
+test("Instances", function() {
+ expect(8);
+
+ equals( typeof Popcorn.addInstance, "function" , "Popcorn.addInstance is a provided utility function");
+ equals( typeof Popcorn.removeInstance, "function" , "Popcorn.removeInstance is a provided utility function");
+ equals( typeof Popcorn.getInstanceById, "function" , "Popcorn.getInstanceById is a provided utility function");
+ equals( typeof Popcorn.instanceIds, "object" , "Popcorn.instanceIds is a provided cache object");
+ ok( "length" in Popcorn.instances && "join" in Popcorn.instances, "Popcorn.error is a provided cache array");
+
+ var instance;
+
+ for ( var prop in Popcorn.instanceIds ) {
+
+ instance = Popcorn.getInstanceById( prop );
+
+ ok( instance.video, "Stored instance as a `video` property" );
+ ok( instance.data, "Stored instance as a `data` property" );
+ ok( instance instanceof Popcorn, "instance instanceof Popcorn" );
+
+ }
+
+});
test("guid", function () {