Mooeypoo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/243726

Change subject: OO.EventEmitter: Allow disconnecting events given by array
......................................................................

OO.EventEmitter: Allow disconnecting events given by array

OO.EventEmitter allows to connect events by array shorthand, it
should also allow to disconnect these events.

Change-Id: I41d50d8034282131ba3512c5cc0d61c4214663bb
---
M src/EventEmitter.js
M tests/unit/EventEmitter.test.js
2 files changed, 34 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/26/243726/1

diff --git a/src/EventEmitter.js b/src/EventEmitter.js
index 7b57193..dbb337f 100644
--- a/src/EventEmitter.js
+++ b/src/EventEmitter.js
@@ -222,12 +222,16 @@
         * @chainable
         */
        oo.EventEmitter.prototype.disconnect = function ( context, methods ) {
-               var i, event, bindings;
+               var i, event, method, bindings;
 
                if ( methods ) {
                        // Remove specific connections to the context
                        for ( event in methods ) {
-                               this.off( event, methods[ event ], context );
+                               method = methods[ event ];
+                               if ( Array.isArray( method ) ) {
+                                       method = method[ 0 ];
+                               }
+                               this.off( event, method, context );
                        }
                } else {
                        // Remove all connections to the context
diff --git a/tests/unit/EventEmitter.test.js b/tests/unit/EventEmitter.test.js
index fdf0637..c3070de 100644
--- a/tests/unit/EventEmitter.test.js
+++ b/tests/unit/EventEmitter.test.js
@@ -251,6 +251,34 @@
                assert.deepEqual( hits, { foo: 1, bar: 2 } );
        } );
 
+       QUnit.test( 'disconnect( host, array methods )', 1, function ( assert ) 
{
+               var host,
+                       hits = { foo: 0, barbara: 0 },
+                       ee = new oo.EventEmitter();
+
+               host = {
+                       onFoo: function () {
+                               hits.foo++;
+                       },
+                       barbara: function () {
+                               hits.barbara++;
+                       }
+               };
+
+               ee.connect( host, {
+                       foo: 'onFoo',
+                       bar: [ 'barbara', 'some', 'parameter' ]
+               } );
+               ee.emit( 'foo' );
+               ee.emit( 'bar' );
+
+               ee.disconnect( host, { bar: [ 'barbara' ] } );
+               ee.emit( 'foo' );
+               ee.emit( 'bar' );
+
+               assert.deepEqual( hits, { foo: 2, barbara: 1 } );
+       } );
+
        QUnit.test( 'disconnect( host, unbound methods )', 1, function ( assert 
) {
                var host,
                        ee = new oo.EventEmitter();

-- 
To view, visit https://gerrit.wikimedia.org/r/243726
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I41d50d8034282131ba3512c5cc0d61c4214663bb
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to