Skip to content
Closed
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
23 changes: 13 additions & 10 deletions angular-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ angular.module("angularWidget").provider("widgets", function() {
};
this.$get = [ "$injector", function($injector) {
var widgets = [];
function notifyInjector(injector, args) {
var scope = injector.get("$rootScope");
var isMe = $injector === injector;
var event;
if (args.length) {
event = scope.$broadcast.apply(scope, args);
}
if (!isMe) {
scope.$digest();
}
return event;
}
return {
getWidgetManifest: manifestGenerator ? $injector.invoke(manifestGenerator) : angular.noop,
unregisterWidget: function(injector) {
Expand All @@ -247,16 +259,7 @@ angular.module("angularWidget").provider("widgets", function() {
notifyWidgets: function() {
var args = arguments;
return widgets.map(function(injector) {
var scope = injector.get("$rootScope");
if (args.length) {
var event;
scope.$apply(function() {
event = scope.$broadcast.apply(scope, args);
});
return event;
} else {
return scope.$digest();
}
return notifyInjector(injector, args);
});
}
};
Expand Down
24 changes: 14 additions & 10 deletions app/scripts/services/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ angular.module('angularWidget')
this.$get = function ($injector) {
var widgets = [];

function notifyInjector(injector, args) {
var scope = injector.get('$rootScope');
var isMe = $injector === injector;
var event;
if (args.length) {
event = scope.$broadcast.apply(scope, args);
}
if (!isMe) {
scope.$digest();
}
return event;
}

return {
getWidgetManifest: manifestGenerator ? $injector.invoke(manifestGenerator) : angular.noop,
unregisterWidget: function (injector) {
Expand All @@ -34,16 +47,7 @@ angular.module('angularWidget')
notifyWidgets: function () {
var args = arguments;
return widgets.map(function (injector) {
var scope = injector.get('$rootScope');
if (args.length) {
var event;
scope.$apply(function () {
event = scope.$broadcast.apply(scope, args);
});
return event;
} else {
return scope.$digest();
}
return notifyInjector(injector, args);
});
}
};
Expand Down
21 changes: 18 additions & 3 deletions test/spec/services/widgets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,29 @@ describe('Unit testing widgets service', function () {
}));

it('should broadcast event when notifyWidgets is invoked with args', inject(function (widgets) {
var applySpy = jasmine.createSpy('$apply').andCallFake(function (fn) { fn(); });
var digestSpy = jasmine.createSpy('$digest');
var broadcastSpy = jasmine.createSpy('$broadcastSpy').andReturn('shahata');
widgets.registerWidget({get: function (name) {
expect(name).toBe('$rootScope');
return {$apply: applySpy, $broadcast: broadcastSpy};
return {$digest: digestSpy, $broadcast: broadcastSpy};
}});
expect(widgets.notifyWidgets(1, 2, 3)).toEqual(['shahata']);
expect(applySpy).toHaveBeenCalled();
expect(digestSpy).toHaveBeenCalled();
expect(broadcastSpy).toHaveBeenCalledWith(1, 2, 3);
}));

it('should not call digest in case the caller injector is himself', inject(function (widgets, $injector) {
var digestSpy = jasmine.createSpy('$digest');
widgets.registerWidget($injector);
widgets.notifyWidgets();
expect(digestSpy).not.toHaveBeenCalled();
}));

it('should call broadcast in case the caller injector is himself', inject(function (widgets, $injector) {
var scope = $injector.get('$rootScope');
var broadcastSpy = spyOn(scope, '$broadcast');
widgets.registerWidget($injector);
widgets.notifyWidgets(1, 2, 3);
expect(broadcastSpy).toHaveBeenCalledWith(1, 2, 3);
}));

Expand Down