mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
fix(delegates): find active instance
This commit is contained in:
9
js/angular/controller/listController.js
vendored
9
js/angular/controller/listController.js
vendored
@@ -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) {
|
||||
|
||||
11
js/angular/controller/scrollController.js
vendored
11
js/angular/controller/scrollController.js
vendored
@@ -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);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
4
js/angular/controller/sideMenuController.js
vendored
4
js/angular/controller/sideMenuController.js
vendored
@@ -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() {
|
||||
|
||||
9
js/angular/directive/slideBox.js
vendored
9
js/angular/directive/slideBox.js
vendored
@@ -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();
|
||||
|
||||
7
js/angular/directive/tabs.js
vendored
7
js/angular/directive/tabs.js
vendored
@@ -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;
|
||||
|
||||
16
js/angular/service/history.js
vendored
16
js/angular/service/history.js
vendored
@@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user