From 60743fc52aea9eabee58258a31f4ba465013cb4e Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Sun, 19 Feb 2012 12:59:10 -0800 Subject: feat(ng:include) Fire $contentLoaded event + refactor unload to listen on this event -> we can use unload with ng:view as well Closes #743 --- src/AngularPublic.js | 1 + src/directives.js | 12 ++++++++++++ src/service/route.js | 2 +- src/widgets.js | 3 +-- test/widgetsSpec.js | 36 ++++++++++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 20ca5edb..e65f1281 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -72,6 +72,7 @@ function publishExternalAPI(angular){ script: scriptTemplateLoader, select: selectDirective, style: styleDirective, + onload: onloadDirective, option: optionDirective, ngBind: ngBindDirective, ngBindHtml: ngBindHtmlDirective, diff --git a/src/directives.js b/src/directives.js index 2d42d3ef..c988bf7b 100644 --- a/src/directives.js +++ b/src/directives.js @@ -975,3 +975,15 @@ var styleDirective = valueFn({ restrict: 'E', terminal: true }); + + +var onloadDirective = valueFn({ + restrict: 'AC', + link: function(scope, elm, attr) { + var onloadExp = attr.onload || ''; //workaround for jquery bug #7537) + + scope.$on('$contentLoaded', function(event) { + scope.$eval(onloadExp); + }); + } +}); diff --git a/src/service/route.js b/src/service/route.js index 8d8086af..50f335a2 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -177,7 +177,7 @@ function $RouteProvider(){ * @ngdoc event * @name angular.module.ng.$route#$routeUpdate * @eventOf angular.module.ng.$route - * @eventType emit on the current route scope + * @eventType broadcast on root scope * @description * * The `reloadOnSearch` property has been set to false, and we are reusing the same diff --git a/src/widgets.js b/src/widgets.js index 6d64730f..a32abb75 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -68,7 +68,6 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile' compile: function(element, attr) { var srcExp = attr.src, scopeExp = attr.scope || '', - onloadExp = attr.onload || '', //workaround for jquery bug #7537 autoScrollExp = attr.autoscroll; if (!element[0]['ng:compiled']) { element[0]['ng:compiled'] = true; @@ -106,7 +105,7 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile' if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) { $anchorScroll(); } - scope.$eval(onloadExp); + scope.$emit('$contentLoaded'); } }).error(clearContent); } else { diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index d9517866..f75d81c2 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -119,6 +119,22 @@ describe('widget', function() { })); + it('should fire $contentLoaded event after linking the content', inject( + function($rootScope, $compile, $templateCache) { + var contentLoadedSpy = jasmine.createSpy('content loaded').andCallFake(function() { + expect(element.text()).toBe('partial content'); + }); + + $templateCache.put('url', [200, 'partial content', {}]); + $rootScope.$on('$contentLoaded', contentLoadedSpy); + + element = $compile('')($rootScope); + $rootScope.$digest(); + + expect(contentLoadedSpy).toHaveBeenCalledOnce(); + })); + + it('should evaluate onload expression when a partial is loaded', inject( putIntoCache('myUrl', 'my partial'), function($rootScope, $compile, $browser) { @@ -620,7 +636,7 @@ describe('widget', function() { describe('ng:view', function() { beforeEach(module(function() { return function($rootScope, $compile) { - element = $compile('')($rootScope); + element = $compile('')($rootScope); }; })); @@ -847,7 +863,7 @@ describe('widget', function() { $routeProvider.when('/foo', {controller: noop, template: 'myUrl1'}); }); - inject(function($route, $rootScope, $location, $templateCache, $browser) { + inject(function($route, $rootScope, $location, $templateCache) { $templateCache.put('myUrl1', [200, 'my partial', {}]); $location.path('/foo'); @@ -1004,6 +1020,22 @@ describe('widget', function() { expect(log).toEqual(['init-foo', 'route-update', 'destroy-foo', 'init-bar']); }); }); + + + it('should evaluate onload expression after linking the content', function() { + module(function($routeProvider) { + $routeProvider.when('/foo', {template: 'tpl.html'}); + }); + + inject(function($templateCache, $location, $rootScope) { + $templateCache.put('tpl.html', [200, '{{1+1}}', {}]); + $rootScope.load = jasmine.createSpy('onload'); + + $location.url('/foo'); + $rootScope.$digest(); + expect($rootScope.load).toHaveBeenCalledOnce(); + }); + }) }); -- cgit v1.2.3