Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the message handler context to be the behavior or module instance #161

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ Box.Application = (function() {

// If onmessage is an object call message handler with the matching key (if any)
if (instance.onmessage !== null && typeof instance.onmessage === 'object' && instance.onmessage.hasOwnProperty(name)) {
instance.onmessage[name](data);
instance.onmessage[name].call(instance, data);

// Otherwise if message name exists in messages call onmessage with name, data
} else if (indexOf(instance.messages || [], name) !== -1) {
instance.onmessage(name, data);
instance.onmessage.call(instance, name, data);
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,19 @@ describe('Box.Application', function() {
Box.Application.broadcast('abc', messageData);
});

it('should provide the instance of the module or behavior to the message handler when a message is received', function() {
Box.Application.addModule('test', sandbox.stub().returns({
onmessage: {
'abc': function() {
assert.isTrue(this.onmessage !== undefined);
}
}
}));
Box.Application.start(testModule);

Box.Application.broadcast('abc');
});

it('should call onmessage of behaviors listening in correct order when defined', function() {
var messageData = {},
moduleMessageSpy = sandbox.spy(),
Expand Down