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
24 changes: 18 additions & 6 deletions angular-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ angular.module("angularWidgetInternal").directive("ngWidget", [ "$http", "$templ
if (!emit && !event.stopPropagation || emit && event.stopPropagation) {
var args = Array.prototype.slice.call(arguments);
args[0] = name;
dst.$apply(function() {
if (fn.apply(dst, args).defaultPrevented) {
event.preventDefault();
}
});
if (dst.$root.$$phase) {
applyHandler(fn, dst, args, event);
} else {
dst.$apply(function() {
applyHandler(fn, dst, args, event);
});
}
}
});
}
function applyHandler(fn, dst, args, event) {
if (fn.apply(dst, args).defaultPrevented) {
event.preventDefault();
}
}
function handleNewInjector() {
var widgetConfig = injector.get("widgetConfig");
var widgetScope = injector.get("$rootScope");
Expand Down Expand Up @@ -253,7 +260,12 @@ angular.module("angularWidgetInternal").value("headElement", document.getElement
return deferred.promise;
}
if (url in requireCache) {
return requireCache[url];
requireCache[url].then(function(res) {
deferred.resolve(res);
}, function(res) {
deferred.reject(res);
});
return deferred.promise;
}
requireCache[url] = deferred.promise;
var fileref;
Expand Down
18 changes: 13 additions & 5 deletions app/scripts/directives/ng-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,23 @@ angular.module('angularWidgetInternal')
if ((!emit && !event.stopPropagation) || (emit && event.stopPropagation)) {
var args = Array.prototype.slice.call(arguments);
args[0] = name;
dst.$apply(function () {
if (fn.apply(dst, args).defaultPrevented) {
event.preventDefault();
}
});
if (dst.$root.$$phase) {
applyHandler(fn, dst, args, event);
} else {
dst.$apply(function () {
applyHandler(fn, dst, args, event);
});
}
}
});
}

function applyHandler(fn, dst, args, event) {
if (fn.apply(dst, args).defaultPrevented) {
event.preventDefault();
}
}

function handleNewInjector() {
var widgetConfig = injector.get('widgetConfig');
var widgetScope = injector.get('$rootScope');
Expand Down
7 changes: 6 additions & 1 deletion app/scripts/services/tag-appender.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ angular.module('angularWidgetInternal')
return deferred.promise;
}
if (url in requireCache) {
return requireCache[url];
requireCache[url].then(function (res) {
deferred.resolve(res);
}, function (res) {
deferred.reject(res);
});
return deferred.promise;
}
requireCache[url] = deferred.promise;

Expand Down
21 changes: 21 additions & 0 deletions test/spec/directives/ng-widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Unit testing ngWidget directive', function () {

widgetsProvider.addServiceToShare('$location');
widgetsProvider.addEventToForward('$locationChangeStart');
widgetsProvider.addEventToForward('someEventToForward');
});
});

Expand Down Expand Up @@ -151,6 +152,26 @@ describe('Unit testing ngWidget directive', function () {
expect(watchSpy).toHaveBeenCalled();
}));

it('should call forward events handler while running main scope\'s $digest cycle', inject(function ($rootScope) {
downloadWidgetSuccess();
compileWidget();
flushDownload();

var widgetScope = widgetInjector.get('$rootScope');
var event1Handler = jasmine.createSpy('$locationChangeStart');
var event2Handler = jasmine.createSpy('someEventToForward');

$rootScope.$on('someEventToForward', event2Handler);
$rootScope.$on('$locationChangeStart', event1Handler.andCallFake(function () {
widgetScope.$emit('someEventToForward');
}));

widgetScope.$emit('$locationChangeStart');

expect(event1Handler).toHaveBeenCalledWith(jasmine.any(Object));
expect(event2Handler).toHaveBeenCalled();
}));

it('should emit events that were declared as forwarded', inject(function ($rootScope) {
downloadWidgetSuccess();
compileWidget();
Expand Down
8 changes: 4 additions & 4 deletions test/spec/services/tag-appender.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ describe('Unit testing tagAppender service', function () {
.toBe('<script type="text/javascript" src="dummy.js"></script>');
}));

it('should return the same promise when same file loads twice simultaneously ', inject (function (tagAppender) {
var firstLoadPromise = tagAppender('dummy.js', 'js');
var secondLoadPromise = tagAppender('dummy.js', 'js');
expect(firstLoadPromise).toEqual(secondLoadPromise);
it('should load the file only once in case the same file is loaded multiple times simultaneously ', inject (function (tagAppender) {
tagAppender('dummy.js', 'js');
tagAppender('dummy.js', 'js');
expect(headElement.appendChild.calls.length).toBe(1);
}));

it('should re try to download the file in case first attempt failed', inject (function (tagAppender) {
Expand Down