fix(delegates): find active instance

This commit is contained in:
Adam Bradley
2014-11-23 22:42:59 -06:00
parent 72167b2a45
commit 0951b97f06
10 changed files with 98 additions and 22 deletions

View File

@@ -78,13 +78,18 @@ IonicModule
'$scope',
'$attrs',
'$ionicListDelegate',
function($scope, $attrs, $ionicListDelegate) {
'$ionicHistory',
function($scope, $attrs, $ionicListDelegate, $ionicHistory) {
var self = this;
var isSwipeable = true;
var isReorderShown = false;
var isDeleteShown = false;
var deregisterInstance = $ionicListDelegate._registerInstance(self, $attrs.delegateHandle);
var deregisterInstance = $ionicListDelegate._registerInstance(
self, $attrs.delegateHandle, function() {
return $ionicHistory.isActiveScope($scope);
}
);
$scope.$on('$destroy', deregisterInstance);
self.showReorder = function(show) {

View File

@@ -32,16 +32,7 @@ function($scope, scrollViewOptions, $timeout, $window, $location, $document, $io
var deregisterInstance = $ionicScrollDelegate._registerInstance(
self, scrollViewOptions.delegateHandle, function() {
if ($scope.$$disconnected) {
return false;
}
var currentHistoryId = $ionicHistory.currentHistoryId();
if (currentHistoryId) {
return currentHistoryId == (isDefined($scope.$historyId) ? $scope.$historyId : 'root');
}
return true;
return $ionicHistory.isActiveScope($scope);
}
);

View File

@@ -397,7 +397,9 @@ function($scope, $attrs, $ionicSideMenuDelegate, $ionicPlatform, $ionicBody, $io
});
var deregisterInstance = $ionicSideMenuDelegate._registerInstance(
self, $attrs.delegateHandle
self, $attrs.delegateHandle, function() {
return $ionicHistory.isActiveScope($scope);
}
);
$scope.$on('$destroy', function() {

View File

@@ -39,7 +39,8 @@ IonicModule
.directive('ionSlideBox', [
'$ionicSlideBoxDelegate',
'$window',
function($ionicSlideBoxDelegate, $window) {
'$ionicHistory',
function($ionicSlideBoxDelegate, $window, $ionicHistory) {
return {
restrict: 'E',
@@ -64,7 +65,11 @@ function($ionicSlideBoxDelegate, $window) {
function postLink(scope, element, attr, slideBoxCtrl) {
element.addClass('slider');
var deregister = $ionicSlideBoxDelegate._registerInstance(slideBoxCtrl, attr.delegateHandle);
var deregister = $ionicSlideBoxDelegate._registerInstance(
slideBoxCtrl, attr.delegateHandle, function() {
return $ionicHistory.isActiveScope(scope);
}
);
watchSelected();
isDefined(attr.loop) && watchLoop();

View File

@@ -47,7 +47,8 @@ IonicModule
.directive('ionTabs', [
'$ionicTabsDelegate',
'$ionicConfig',
function($ionicTabsDelegate, $ionicConfig) {
'$ionicHistory',
function($ionicTabsDelegate, $ionicConfig, $ionicHistory) {
return {
restrict: 'E',
scope: true,
@@ -65,7 +66,9 @@ function($ionicTabsDelegate, $ionicConfig) {
return { pre: prelink, post: postLink };
function prelink($scope, $element, $attr, tabsCtrl) {
var deregisterInstance = $ionicTabsDelegate._registerInstance(
tabsCtrl, $attr.delegateHandle
tabsCtrl, $attr.delegateHandle, function() {
return $ionicHistory.isActiveScope($scope);
}
);
tabsCtrl.$scope = $scope;

View File

@@ -441,7 +441,10 @@ function($rootScope, $state, $location, $window, $ionicViewSwitcher, $ionicNavVi
* @description The app's current view.
* @returns {object} Returns the current view.
*/
currentView: function() {
currentView: function(view) {
if (arguments.length) {
viewHistory.currentView = view;
}
return viewHistory.currentView;
},
@@ -636,6 +639,17 @@ function($rootScope, $state, $location, $window, $ionicViewSwitcher, $ionicNavVi
isAbstractEle: function(ele) {
return !!(ele && (isAbstractTag(ele) || isAbstractTag(ele.children())));
},
isActiveScope: function(scope) {
if (!scope || scope.$$disconnected) return false;
var currentHistoryId = this.currentHistoryId();
if (currentHistoryId) {
return currentHistoryId == (isDefined(scope.$historyId) ? scope.$historyId : 'root');
}
return true;
}
};

View File

@@ -17,7 +17,7 @@ describe('$ionicList controller', function() {
spyOn($ionicListDelegate, '_registerInstance');
var ctrl = setup({delegateHandle: 'foobar'});
expect($ionicListDelegate._registerInstance)
.toHaveBeenCalledWith(ctrl, 'foobar');
.toHaveBeenCalledWith(ctrl, 'foobar', jasmine.any(Function));
}));
it('should register with given handle and deregister on destroy', inject(function($ionicListDelegate) {
@@ -29,7 +29,7 @@ describe('$ionicList controller', function() {
delegateHandle: 'something'
});
expect($ionicListDelegate._registerInstance)
.toHaveBeenCalledWith(ctrl, 'something');
.toHaveBeenCalledWith(ctrl, 'something', jasmine.any(Function));
expect(deregisterSpy).not.toHaveBeenCalled();
ctrl.$scope.$destroy();

View File

@@ -17,7 +17,7 @@ describe('Ionic Angular Side Menu', function() {
expect(el.controller('ionSideMenus')).toBeDefined();
expect($ionicSideMenuDelegate._registerInstance)
.toHaveBeenCalledWith(el.controller('ionSideMenus'), 'superHandle');
.toHaveBeenCalledWith(el.controller('ionSideMenus'), 'superHandle', jasmine.any(Function));
expect(deregisterSpy).not.toHaveBeenCalled();
el.scope().$destroy();

View File

@@ -206,7 +206,7 @@ describe('tabs', function() {
var el = setup('delegate-handle="banana"');
expect($ionicTabsDelegate._registerInstance)
.toHaveBeenCalledWith(el.controller('ionTabs'), 'banana');
.toHaveBeenCalledWith(el.controller('ionTabs'), 'banana', jasmine.any(Function));
expect(deregisterSpy).not.toHaveBeenCalled();
el.scope().$destroy();

View File

@@ -1108,6 +1108,62 @@ describe('Ionic History', function() {
expect(newView.stateName).toEqual('about');
}));
it('should not be active when scope null', function() {
expect(ionicHistory.isActiveScope(null)).toEqual(false);
expect(ionicHistory.isActiveScope(undefined)).toEqual(false);
expect(ionicHistory.isActiveScope()).toEqual(false);
});
it('should not be active when scope disconnected', function() {
var scope = {
$$disconnected: true
};
expect(ionicHistory.isActiveScope(scope)).toEqual(false);
});
it('should be active w/ scope but no current history id', function() {
ionicHistory.registerHistory('1234');
expect(ionicHistory.isActiveScope(scope)).toEqual(true);
});
it('should be active w/ scope same history id as current view', function() {
ionicHistory.currentView({
historyId: '123'
});
var scope = {
$historyId: '123'
}
expect(ionicHistory.isActiveScope(scope)).toEqual(true);
});
it('should be not active w/ scope different history id as current view', function() {
ionicHistory.currentView({
historyId: '123'
});
var scope = {
$historyId: 'abc'
}
expect(ionicHistory.isActiveScope(scope)).toEqual(false);
});
it('should be active when scope w/ unknown history and current view root history id', function() {
ionicHistory.currentView({
historyId: 'root'
});
expect(ionicHistory.isActiveScope({})).toEqual(true);
});
it('should not be active when scope w/ unknown history and current view not root history id', function() {
ionicHistory.currentView({
historyId: '123'
});
expect(ionicHistory.isActiveScope({})).toEqual(false);
});
it('should go() to a view', inject(function($location) {
var newView = ionicHistory.createView({ stateName: 'about' });
newView.go();