From: unknown Date: Fri, 11 Feb 2011 15:23:42 +0000 (-0500) Subject: fixed white space and requested changes X-Git-Tag: v0.4~26^2~7 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=13c63bf2ce6a117ab5f36b0d70fc6c9e1dbc856a;p=popcorn-js.git fixed white space and requested changes added unit test --- diff --git a/popcorn.js b/popcorn.js index 2e80729..1d07986 100644 --- a/popcorn.js +++ b/popcorn.js @@ -24,19 +24,35 @@ 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]]; }; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a1f131e..6b72db7 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -68,6 +68,28 @@ test("Utility", function () { }); +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 () {