Skip to content

Commit

Permalink
fix(viewController): use $watch instead of $observe
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Nov 13, 2014
1 parent ed8bd94 commit b75bd9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions js/angular/controller/viewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function($scope, $element, $attrs, $compile, $ionicHistory, $ionicViewSwitcher)
// add listeners for when this view changes
$scope.$on('$ionicView.beforeEnter', self.beforeEnter);
$scope.$on('$ionicView.afterEnter', afterEnter);
$scope.$on('$ionicView.beforeLeave', deregisterObservers);
$scope.$on('$ionicView.beforeLeave', deregisterFns);
};

self.beforeEnter = function(ev, transData) {
Expand Down Expand Up @@ -72,7 +72,7 @@ function($scope, $element, $attrs, $compile, $ionicHistory, $ionicViewSwitcher)
});

// make sure any existing observers are cleaned up
deregisterObservers();
deregisterFns();
}
};

Expand All @@ -89,22 +89,22 @@ function($scope, $element, $attrs, $compile, $ionicHistory, $ionicViewSwitcher)
}

if (isDefined($attrs.hideBackButton)) {
deregisters.push($attrs.$observe('hideBackButton', function() {
navViewCtrl.showBackButton(!attrTrue('hideBackButton'));
deregisters.push($scope.$watch($attrs.hideBackButton, function(val) {
navViewCtrl.showBackButton(!val);
}));
}

if (isDefined($attrs.hideNavBar)) {
deregisters.push($attrs.$observe('hideNavBar', function() {
navViewCtrl.showBar(!attrTrue('hideNavBar'));
deregisters.push($scope.$watch($attrs.hideNavBar, function(val) {
navViewCtrl.showBar(!val);
}));
}

$ionicViewSwitcher.setActiveView($element.parent());
}


function deregisterObservers() {
function deregisterFns() {
// remove all existing $attrs.$observe's
for (var x = 0; x < deregisters.length; x++) {
deregisters[x]();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/angular/directive/view.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('ionView directive', function() {
}));

it('should only observe hideNavBar attr after afterEnter and before beforeLeave', inject(function($rootScope) {
var el = setup('hide-nav-bar="{{ hide }}"', {hide: false});
var el = setup('hide-nav-bar="hide"', {hide: false});
$rootScope.$broadcast('$ionicView.beforeEnter', {});
var spy = el.data('$ionNavViewController').showBar;
expect(spy).not.toHaveBeenCalled();
Expand All @@ -165,7 +165,7 @@ describe('ionView directive', function() {
}));

it('should only observe hideBackButton attr after afterEnter and before beforeLeave', inject(function($rootScope) {
var el = setup('hide-back-button="{{ hide }}"', {hide: false});
var el = setup('hide-back-button="hide"', {hide: false});
$rootScope.$broadcast('$ionicView.beforeEnter', {});
var spy = el.data('$ionNavViewController').showBackButton;
expect(spy).not.toHaveBeenCalled();
Expand Down

0 comments on commit b75bd9d

Please sign in to comment.