From 2c029e40ad7971ac1593486936996230f294ee6d Mon Sep 17 00:00:00 2001 From: Vegard Stene Date: Mon, 15 Feb 2016 10:47:39 +0100 Subject: [PATCH 1/2] Increase readability --- angularjs-viewhead.js | 136 +++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 75 deletions(-) diff --git a/angularjs-viewhead.js b/angularjs-viewhead.js index 98a5213..b6af952 100644 --- a/angularjs-viewhead.js +++ b/angularjs-viewhead.js @@ -1,76 +1,62 @@ - (function (angular) { - - var mod = angular.module('viewhead', []); - - var title; - - mod.directive( - 'viewTitle', - ['$rootScope', '$timeout', function ($rootScope, $timeout) { - return { - restrict: 'EA', - link: function (scope, iElement, iAttrs, controller, transcludeFn) { - // If we've been inserted as an element then we detach from the DOM because the caller - // doesn't want us to have any visual impact in the document. - // Otherwise, we're piggy-backing on an existing element so we'll just leave it alone. - var tagName = iElement[0].tagName.toLowerCase(); - if (tagName === 'view-title' || tagName === 'viewtitle') { - iElement.remove(); - } - - scope.$watch( - function () { - return iElement.text(); - }, - function (newTitle) { - $rootScope.viewTitle = title = newTitle; - } - ); - scope.$on( - '$destroy', - function () { - title = undefined; - // Wait until next digest cycle do delete viewTitle - $timeout(function() { - if(!title) { - // No other view-title has reassigned title. - delete $rootScope.viewTitle; - } - }); - } - ); - } - }; - }] - ); - - mod.directive( - 'viewHead', - ['$document', function ($document) { - var head = angular.element($document[0].head); - return { - restrict: 'A', - link: function (scope, iElement, iAttrs, controller, transcludeFn) { - // Move the element into the head of the document. - // Although the physical location of the document changes, the element remains - // bound to the scope in which it was declared, so it can refer to variables from - // the view scope if necessary. - head.append(iElement); - - // When the scope is destroyed, remove the element. - // This is on the assumption that we're being used in some sort of view scope. - // It doesn't make sense to use this directive outside of the view, and nor does it - // make sense to use it inside other scope-creating directives like ng-repeat. - scope.$on( - '$destroy', - function () { - iElement.remove(); - } - ); - } - }; - }] - ); - -})(angular); + var mod = angular.module('viewhead', []); + + var title; + + mod.directive('viewTitle', ['$rootScope', '$timeout', function ($rootScope, $timeout) { + return { + restrict: 'EA', + link: function (scope, iElement) { + // If we've been inserted as an element then we detach from the DOM because the caller + // doesn't want us to have any visual impact in the document. + // Otherwise, we're piggy-backing on an existing element so we'll just leave it alone. + var tagName = iElement[0].tagName.toLowerCase(); + + if (tagName === 'view-title' || tagName === 'viewtitle') { + iElement.remove(); + } + + scope.$watch(function () { + return iElement.text(); + }, function (newTitle) { + $rootScope.viewTitle = title = newTitle; + }); + + scope.$on('$destroy', function () { + title = undefined; + + // Wait until next digest cycle do delete viewTitle + $timeout(function () { + if (!title) { + // No other view-title has reassigned title. + delete $rootScope.viewTitle; + } + }); + }); + } + }; + }]); + + mod.directive('viewHead', ['$document', function ($document) { + var head = angular.element($document[0].head); + + return { + restrict: 'A', + link: function (scope, iElement) { + // Move the element into the head of the document. + // Although the physical location of the document changes, the element remains + // bound to the scope in which it was declared, so it can refer to variables from + // the view scope if necessary. + head.append(iElement); + + // When the scope is destroyed, remove the element. + // This is on the assumption that we're being used in some sort of view scope. + // It doesn't make sense to use this directive outside of the view, and nor does it + // make sense to use it inside other scope-creating directives like ng-repeat. + scope.$on('$destroy', function () { + iElement.remove(); + }); + } + }; + }]); +})(angular); \ No newline at end of file From 533a18bb00646958bea014d5f02c5a51be771ff7 Mon Sep 17 00:00:00 2001 From: Vegard Stene Date: Mon, 15 Feb 2016 11:32:31 +0100 Subject: [PATCH 2/2] If the view title has been updated by another view title directive, don't remove it --- angularjs-viewhead.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/angularjs-viewhead.js b/angularjs-viewhead.js index b6af952..ec11084 100644 --- a/angularjs-viewhead.js +++ b/angularjs-viewhead.js @@ -1,9 +1,14 @@ (function (angular) { + 'use strict'; + var mod = angular.module('viewhead', []); - var title; + // Data service that is shared between all directives + mod.factory('viewTitleDataService', function () { + return {}; + }); - mod.directive('viewTitle', ['$rootScope', '$timeout', function ($rootScope, $timeout) { + mod.directive('viewTitle', ['$rootScope', 'viewTitleDataService', function ($rootScope, viewTitleDataService) { return { restrict: 'EA', link: function (scope, iElement) { @@ -19,19 +24,18 @@ scope.$watch(function () { return iElement.text(); }, function (newTitle) { - $rootScope.viewTitle = title = newTitle; + viewTitleDataService.id = scope.$id; + + $rootScope.viewTitle = newTitle; }); scope.$on('$destroy', function () { - title = undefined; - - // Wait until next digest cycle do delete viewTitle - $timeout(function () { - if (!title) { - // No other view-title has reassigned title. - delete $rootScope.viewTitle; - } - }); + // Only remove view title if the current title has been set by this scope + if (viewTitleDataService.id === scope.$id) { + delete $rootScope.viewTitle; + + delete viewTitleDataService.id; + } }); } };