Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 60743fc

Browse files
committed
feat(ng:include) Fire $contentLoaded event
+ refactor unload to listen on this event -> we can use unload with ng:view as well Closes #743
1 parent 9486590 commit 60743fc

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

src/AngularPublic.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function publishExternalAPI(angular){
7272
script: scriptTemplateLoader,
7373
select: selectDirective,
7474
style: styleDirective,
75+
onload: onloadDirective,
7576
option: optionDirective,
7677
ngBind: ngBindDirective,
7778
ngBindHtml: ngBindHtmlDirective,

src/directives.js

+12
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,15 @@ var styleDirective = valueFn({
975975
restrict: 'E',
976976
terminal: true
977977
});
978+
979+
980+
var onloadDirective = valueFn({
981+
restrict: 'AC',
982+
link: function(scope, elm, attr) {
983+
var onloadExp = attr.onload || ''; //workaround for jquery bug #7537)
984+
985+
scope.$on('$contentLoaded', function(event) {
986+
scope.$eval(onloadExp);
987+
});
988+
}
989+
});

src/service/route.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function $RouteProvider(){
177177
* @ngdoc event
178178
* @name angular.module.ng.$route#$routeUpdate
179179
* @eventOf angular.module.ng.$route
180-
* @eventType emit on the current route scope
180+
* @eventType broadcast on root scope
181181
* @description
182182
*
183183
* The `reloadOnSearch` property has been set to false, and we are reusing the same

src/widgets.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
6868
compile: function(element, attr) {
6969
var srcExp = attr.src,
7070
scopeExp = attr.scope || '',
71-
onloadExp = attr.onload || '', //workaround for jquery bug #7537
7271
autoScrollExp = attr.autoscroll;
7372
if (!element[0]['ng:compiled']) {
7473
element[0]['ng:compiled'] = true;
@@ -106,7 +105,7 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
106105
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
107106
$anchorScroll();
108107
}
109-
scope.$eval(onloadExp);
108+
scope.$emit('$contentLoaded');
110109
}
111110
}).error(clearContent);
112111
} else {

test/widgetsSpec.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ describe('widget', function() {
119119
}));
120120

121121

122+
it('should fire $contentLoaded event after linking the content', inject(
123+
function($rootScope, $compile, $templateCache) {
124+
var contentLoadedSpy = jasmine.createSpy('content loaded').andCallFake(function() {
125+
expect(element.text()).toBe('partial content');
126+
});
127+
128+
$templateCache.put('url', [200, 'partial content', {}]);
129+
$rootScope.$on('$contentLoaded', contentLoadedSpy);
130+
131+
element = $compile('<ng:include src="\'url\'"></ng:include>')($rootScope);
132+
$rootScope.$digest();
133+
134+
expect(contentLoadedSpy).toHaveBeenCalledOnce();
135+
}));
136+
137+
122138
it('should evaluate onload expression when a partial is loaded', inject(
123139
putIntoCache('myUrl', 'my partial'),
124140
function($rootScope, $compile, $browser) {
@@ -620,7 +636,7 @@ describe('widget', function() {
620636
describe('ng:view', function() {
621637
beforeEach(module(function() {
622638
return function($rootScope, $compile) {
623-
element = $compile('<ng:view></ng:view>')($rootScope);
639+
element = $compile('<ng:view onload="load()"></ng:view>')($rootScope);
624640
};
625641
}));
626642

@@ -847,7 +863,7 @@ describe('widget', function() {
847863
$routeProvider.when('/foo', {controller: noop, template: 'myUrl1'});
848864
});
849865

850-
inject(function($route, $rootScope, $location, $templateCache, $browser) {
866+
inject(function($route, $rootScope, $location, $templateCache) {
851867
$templateCache.put('myUrl1', [200, 'my partial', {}]);
852868
$location.path('/foo');
853869

@@ -1004,6 +1020,22 @@ describe('widget', function() {
10041020
expect(log).toEqual(['init-foo', 'route-update', 'destroy-foo', 'init-bar']);
10051021
});
10061022
});
1023+
1024+
1025+
it('should evaluate onload expression after linking the content', function() {
1026+
module(function($routeProvider) {
1027+
$routeProvider.when('/foo', {template: 'tpl.html'});
1028+
});
1029+
1030+
inject(function($templateCache, $location, $rootScope) {
1031+
$templateCache.put('tpl.html', [200, '{{1+1}}', {}]);
1032+
$rootScope.load = jasmine.createSpy('onload');
1033+
1034+
$location.url('/foo');
1035+
$rootScope.$digest();
1036+
expect($rootScope.load).toHaveBeenCalledOnce();
1037+
});
1038+
})
10071039
});
10081040

10091041

0 commit comments

Comments
 (0)