Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
fix(off): Allows no eventName
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin MacDonald committed Apr 9, 2014
1 parent b7c602e commit 74de512
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 90 deletions.
4 changes: 2 additions & 2 deletions docs/v1/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ connection!
1. [Code Example](#code-example)
2. [$goConnection#$connect](#$goconnection#$connect)
3. [$goConnection#$set](#$goconnection#$set)
4. [$goConnection#$loginUrl](#$goconnection#$loginUrl)
5. [$goConnection#$logoutUrl](#$goconnection#$logoutUrl)
4. [$goConnection#$loginUrl](#$goconnection#$loginurl)
5. [$goConnection#$logoutUrl](#$goconnection#$logouturl)
6. [$goConnection#$ready](#$goconnection#$ready)

## Code Example
Expand Down
2 changes: 1 addition & 1 deletion docs/v1/model/key_model/off.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ this model or from the model itself.
| optionsObject |
|:---|
| Type: [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |
| An object with three properties, `listener`, `local` and `bubble`, that determine how this action behaves. |
| An object with two properties, `local` and `bubble`, that determines how this action behaves. |
| - `local` [**default: false**] is a boolean where, if true, the listener will be triggered for local events. |
| - `bubble` [**default: false**] is a boolean where, if true, the listener will bubble events. |

Expand Down
2 changes: 1 addition & 1 deletion docs/v1/model/users_model/on.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ triggered.
| optionsObject |
|:---|
| Type: [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |
| An object with three properties, `listener`, `local` and `bubble`, that determine how this action behaves. |
| An object with two properties, `local` and `bubble`, that determines how this action behaves. |
| - `local` [**default: false**] is a boolean where, if true, the listener will be triggered for local events. |
| - `bubble` [**default: false**] is a boolean where, if true, the listener will bubble events. |

Expand Down
18 changes: 11 additions & 7 deletions lib/key_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,26 @@ KeyModel.prototype.$remove = function(opts) {
* @extends Model#$on
*/
KeyModel.prototype.$on = function(eventName, opts, listener) {
if (!_.contains(KEY_EVENTS, eventName)) {
return Model.prototype.$on.call(this, eventName, opts || listener);
if (_.contains(KEY_EVENTS, eventName)) {
return this.$$key.on(eventName, opts, listener);
}

this.$$key.on(eventName, opts, listener);
Model.prototype.$on.call(this, eventName, opts);
};

/**
* Remove a listener on this key
* Remove a listener on this key. If the eventName is empty call off on both the
* key and the model (Model.prototype.off).
* @public
* @extends Model#$off
*/
KeyModel.prototype.$off = function(eventName, opts, listener) {
if (!_.contains(KEY_EVENTS, eventName)) {
return Model.prototype.$on.call(this, eventName, opts || listener);
if (_.contains(KEY_EVENTS, eventName)) {
return this.$$key.off(eventName, opts, listener);

} else if (!eventName) {
this.$$key.off(eventName, opts, listener);
}

this.$$key.off(eventName, opts, listener);
Model.prototype.$off.call(this, eventName, opts);
};
4 changes: 0 additions & 4 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,5 @@ Model.prototype.$on = function(eventName, listener) {
* @public
*/
Model.prototype.$off = function(eventName, listener) {
if (!_.contains(LOCAL_EVENTS, eventName)) {
throw new Error('Invalid event name: ' + eventName);
}

this.$$emitter.off(eventName, listener);
};
34 changes: 13 additions & 21 deletions lib/users_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,31 @@ UsersModel.prototype.$self = function(sync) {
UsersModel.prototype.$getUser = KeyModel.prototype.$key;

/**
* Bind a listener to events on this key
* Bind a listener to events on this room
* @public
* @extends Model#on
* @extends KeyModel#on
*/
UsersModel.prototype.$on = function(eventName, opts, listener) {
if (eventName === SELF) {
return this.$$emitter.on(eventName, opts || listener);
if (_.contains(ROOM_EVENTS, eventName)) {
return this.$$key.room().on(eventName, opts, listener);
}

if (!_.contains(ROOM_EVENTS, eventName)) {
if (!_.isFunction(opts)) {
_.extend(opts, { bubble: true });
}

return KeyModel.prototype.$on.call(this, eventName, opts, listener);
}

this.$$key.room().on(eventName, opts, listener);
KeyModel.prototype.$on.call(this, eventName, opts, listener);
};

/**
* Remove a listener on this key
* Remove a listener on this room. If the eventName is empty, call off on both
* the room and key model (KeyModel.prototype.off).
* @public
* @extends Model#off
* @extends KeyModel#off
*/
UsersModel.prototype.$off = function(eventName, opts, listener) {
if (!_.contains(ROOM_EVENTS, eventName)) {
if (!_.isFunction(opts)) {
_.extend(opts, { bubble: true });
}
if (_.contains(ROOM_EVENTS, eventName)) {
return this.$$key.room().off(eventName, opts, listener);

return KeyModel.prototype.$off.call(this, eventName, opts, listener);
} else if (!eventName) {
this.$$key.room().off(eventName, opts, listener);
}

this.$$key.room().off(eventName, opts, listener);
KeyModel.prototype.$off.call(this, eventName, opts, listener);
};
54 changes: 0 additions & 54 deletions tests/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ describe('GoAngular.Model', function() {
});

describe('$off', function() {
it('throws on invalid eventName', function() {
assert.exception(function() {
model.$off('fakeEvent', sinon.stub());
}, 'Invalid event name: fakeEvent');
});

it('accepts valid events', function() {
sinon.spy(model.$$emitter, 'off');

Expand Down Expand Up @@ -294,30 +288,6 @@ describe('GoAngular.Model', function() {
sinon.assert.calledWith(fakeRoom.on, 'join', fakeListener);
sinon.assert.calledWith(fakeRoom.on, 'leave', fakeOpts, fakeListener);
});

it('adds bubble:true if opts passed', function() {
var fakeListener = sinon.stub();
var fakeOpts = {
local:true
};

var expected = _.clone(fakeOpts);
expected.bubble = true;

model.$on('set', fakeOpts, fakeListener);

sinon.assert.calledOnce(onSpy);
sinon.assert.calledWith(onSpy, 'set', expected, fakeListener);
});

it('does not add bubble:true if opts is function', function() {
var fakeListener = sinon.stub();

model.$on('error', fakeListener);

sinon.assert.calledOnce(onSpy);
sinon.assert.calledWith(onSpy, 'error', fakeListener);
});
});

describe('$off', function() {
Expand All @@ -341,30 +311,6 @@ describe('GoAngular.Model', function() {
sinon.assert.calledWith(fakeRoom.off, 'leave', fakeOpts,
fakeListener);
});

it('adds bubble:true if opts passed', function() {
var fakeListener = sinon.stub();
var fakeOpts = {
local:true
};

var expected = _.clone(fakeOpts);
expected.bubble = true;

model.$off('set', fakeOpts, fakeListener);

sinon.assert.calledOnce(offSpy);
sinon.assert.calledWith(offSpy, 'set', expected, fakeListener);
});

it('does not add bubble:true if opts is function', function() {
var fakeListener = sinon.stub();

model.$off('error', fakeListener);

sinon.assert.calledOnce(offSpy);
sinon.assert.calledWith(offSpy, 'error', fakeListener);
});
});
});
});

0 comments on commit 74de512

Please sign in to comment.