Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
fix(util): Fix the order that event handlers are registered
Browse files Browse the repository at this point in the history
Fix the order that event handlers are registered so the code can work
with both Angular 1.4- and 1.5+. This commit should solve the issue
introduced by the latest release (3.1.0).
  • Loading branch information
mbenford committed May 27, 2016
1 parent 5e8d652 commit 0e3eff4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
return $scope.templateScope;
},
on: function(name, handler) {
$scope.events.on(name, handler);
$scope.events.on(name, handler, true);
return this;
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ tagsInput.factory('tiUtil', function($timeout, $q) {
self.simplePubSub = function() {
var events = {};
return {
on: function(names, handler) {
on: function(names, handler, first) {
names.split(' ').forEach(function(name) {
if (!events[name]) {
events[name] = [];
}
events[name].unshift(handler);
var method = first ? [].unshift : [].push;
method.call(events[name], handler);
});
return this;
},
Expand Down
2 changes: 1 addition & 1 deletion test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ describe('tags-input directive', function() {

// Assert
expect(obj).toBe(autocompleteObj);
expect(isolateScope.events.on).toHaveBeenCalledWith('dummy event', callback);
expect(isolateScope.events.on).toHaveBeenCalledWith('dummy event', callback, true);
});
});

Expand Down
78 changes: 75 additions & 3 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe('tiUtil factory', function() {
expect(callback).toHaveBeenCalledWith('some data');
});

it('subscribes to an event in reverse order', function() {
// Arrange
var callback = jasmine.createSpy();

// Act
sut.on('foo', callback, true);
sut.trigger('foo', 'some data');

// Assert
expect(callback).toHaveBeenCalledWith('some data');
});

it('subscribes to multiple events', function() {
// Arrange
var callback = jasmine.createSpy();
Expand Down Expand Up @@ -59,19 +71,79 @@ describe('tiUtil factory', function() {
expect(callback2).toHaveBeenCalledWith('some data');
});

it('stops the propagation of an event', function() {
it('subscribes multiple times to the same event in reverse order', function() {
// Arrange
var callback1 = jasmine.createSpy(),
callback2 = jasmine.createSpy().and.returnValue(false);
callback2 = jasmine.createSpy();

// Act
sut.on('foo', callback1);
sut.on('foo', callback2);
sut.on('foo', callback2, true);
sut.trigger('foo', 'some data');

// Assert
expect(callback1).toHaveBeenCalledWith('some data');
expect(callback2).toHaveBeenCalledWith('some data');
});

it('guarantees the order of invocation is correct (regular order)', function() {
// Arrange
var calls = [],
callback1 = function() { calls.push('callback1'); },
callback2 = function() { calls.push('callback2'); };

// Act
sut.on('foo', callback1);
sut.on('foo', callback2);
sut.trigger('foo', 'some data');

// Assert
expect(calls).toEqual(['callback1', 'callback2']);
});

it('guarantees the order of invocation is correct (reverse order)', function() {
// Arrange
var calls = [],
callback1 = function() { calls.push('callback1'); },
callback2 = function() { calls.push('callback2'); };

// Act
sut.on('foo', callback1);
sut.on('foo', callback2, true);
sut.trigger('foo', 'some data');

// Assert
expect(calls).toEqual(['callback2', 'callback1']);
});

it('stops the propagation of an event (regular order)', function() {
// Arrange
var callback1 = jasmine.createSpy().and.returnValue(false),
callback2 = jasmine.createSpy();

// Act
sut.on('foo', callback1);
sut.on('foo', callback2);
sut.trigger('foo', 'some data');

// Assert
expect(callback1).toHaveBeenCalledWith('some data');
expect(callback2).not.toHaveBeenCalled();
});

it('stops the propagation of an event (reverse order)', function() {
// Arrange
var callback1 = jasmine.createSpy(),
callback2 = jasmine.createSpy().and.returnValue(false);

// Act
sut.on('foo', callback1);
sut.on('foo', callback2, true);
sut.trigger('foo', 'some data');

// Assert
expect(callback1).not.toHaveBeenCalled();
expect(callback2).toHaveBeenCalledWith('some data');
});

it('returns the object instance so calls can be chained', function() {
Expand Down

0 comments on commit 0e3eff4

Please sign in to comment.