From: rwldrn Date: Sat, 4 Dec 2010 17:10:56 +0000 (-0500) Subject: Event handler reference storage; avoids duplicate event calls; implemented unlisten() X-Git-Tag: v0.2~74 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=f77ff5af4d06e7d56e9d25dfbc4cb22e211a3758;p=popcorn-js.git Event handler reference storage; avoids duplicate event calls; implemented unlisten() --- diff --git a/popcorn-api-draft.js b/popcorn-api-draft.js index fd1ea20..20d612f 100644 --- a/popcorn-api-draft.js +++ b/popcorn-api-draft.js @@ -194,28 +194,35 @@ }, 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 () { @@ -226,7 +233,7 @@ }; // 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]; }); diff --git a/test/popcorn-api-draft-unit.js b/test/popcorn-api-draft-unit.js index e18d2f0..9d92f0c 100644 --- a/test/popcorn-api-draft-unit.js +++ b/test/popcorn-api-draft-unit.js @@ -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;