Skip to content

Commit c99427a

Browse files
author
Adam Bradley
committed
fix(history): Separate histories and views, clear other views in clearHistory(), closes #724
1 parent 668c646 commit c99427a

File tree

3 files changed

+161
-12
lines changed

3 files changed

+161
-12
lines changed

js/ext/angular/src/service/ionicView.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
77
// init the variables that keep track of the view history
88
$rootScope.$viewHistory = {
99
histories: { root: { historyId: 'root', parentHistoryId: null, stack: [], cursor: -1 } },
10+
views: {},
1011
backView: null,
1112
forwardView: null,
1213
currentView: null,
@@ -199,7 +200,7 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
199200
if(forwardView && currentView.stateId !== forwardView.stateId) {
200201
// they navigated to a new view but the stack already has a forward view
201202
// since its a new view remove any forwards that existed
202-
var forwardsHistory = this._getView(forwardView.historyId);
203+
var forwardsHistory = this._getHistoryById(forwardView.historyId);
203204
if(forwardsHistory) {
204205
// the forward has a history
205206
for(var x=forwardsHistory.stack.length - 1; x >= forwardView.index; x--) {
@@ -215,8 +216,8 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
215216
rsp.navAction = 'initialView';
216217
}
217218

218-
// add the new view to the stack
219-
viewHistory.histories[rsp.viewId] = this.createView({
219+
// add the new view
220+
viewHistory.views[rsp.viewId] = this.createView({
220221
viewId: rsp.viewId,
221222
index: hist.stack.length,
222223
historyId: hist.historyId,
@@ -230,7 +231,7 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
230231
});
231232

232233
// add the new view to this history's stack
233-
hist.stack.push(viewHistory.histories[rsp.viewId]);
234+
hist.stack.push(viewHistory.views[rsp.viewId]);
234235
}
235236

236237
this.setNavViews(rsp.viewId);
@@ -243,7 +244,7 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
243244
setNavViews: function(viewId) {
244245
var viewHistory = $rootScope.$viewHistory;
245246

246-
viewHistory.currentView = this._getView(viewId);
247+
viewHistory.currentView = this._getViewById(viewId);
247248
viewHistory.backView = this._getBackView(viewHistory.currentView);
248249
viewHistory.forwardView = this._getForwardView(viewHistory.currentView);
249250

@@ -335,16 +336,20 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
335336
}
336337
},
337338

338-
_getView: function(viewId) {
339-
return (viewId ? $rootScope.$viewHistory.histories[ viewId ] : null );
339+
_getViewById: function(viewId) {
340+
return (viewId ? $rootScope.$viewHistory.views[ viewId ] : null );
340341
},
341342

342343
_getBackView: function(view) {
343-
return (view ? this._getView(view.backViewId) : null );
344+
return (view ? this._getViewById(view.backViewId) : null );
344345
},
345346

346347
_getForwardView: function(view) {
347-
return (view ? this._getView(view.forwardViewId) : null );
348+
return (view ? this._getViewById(view.forwardViewId) : null );
349+
},
350+
351+
_getHistoryById: function(historyId) {
352+
return (historyId ? $rootScope.$viewHistory.histories[ historyId ] : null );
348353
},
349354

350355
_getHistory: function(scope) {
@@ -493,11 +498,11 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
493498
},
494499

495500
clearHistory: function() {
496-
var historyId, x, view,
501+
var
497502
histories = $rootScope.$viewHistory.histories,
498503
currentView = $rootScope.$viewHistory.currentView;
499504

500-
for(historyId in histories) {
505+
for(var historyId in histories) {
501506

502507
if(histories[historyId].stack) {
503508
histories[historyId].stack = [];
@@ -514,6 +519,12 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
514519

515520
}
516521

522+
for(var viewId in $rootScope.$viewHistory.views) {
523+
if(viewId !== currentView.viewId) {
524+
delete $rootScope.$viewHistory.views[viewId];
525+
}
526+
}
527+
517528
this.setNavViews(currentView.viewId);
518529
}
519530

js/ext/angular/test/history.html

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
5+
<link rel="stylesheet" href="../../../../dist/css/ionic.css">
6+
<script src="../../../../dist/js/ionic.bundle.js"></script>
7+
</head>
8+
9+
<body ng-app="starter" animation="slide-left-right-ios7">
10+
<ion-nav-bar type="bar-light" animation="nav-title-slide-ios7" back-button-type="button-icon button-clear" back-button-icon="ion-ios7-arrow-back" back-button-label="Back"></ion-nav-bar>
11+
12+
<ion-nav-view></ion-nav-view>
13+
14+
<script>
15+
angular.module('starter', ['ionic', 'starter.services', 'starter.controllers'])
16+
.run(function($templateCache){
17+
$templateCache.put("templates/about.html","<ion-view title=\"'About Ionic'\"><ion-content has-header=\"true\" has-tabs=\"true\" padding=\"true\"></ion-content></ion-view>");
18+
19+
$templateCache.put("templates/tabs.html","<ion-tabs tabs-style=\"tabs-icon-top\" tabs-type=\"tabs-default\"><ion-tab title=\"Pets\" icon=\"icon ion-home\" href=\"#/tab/pets\"><ion-nav-view name=\"pets-tab\"></ion-nav-view></ion-tab><ion-tab title=\"About\" icon=\"icon ion-search\" href=\"#/tab/about\"><ion-nav-view name=\"about-tab\"></ion-nav-view></ion-tab></ion-tabs>");
20+
$templateCache.put("templates/pet-index.html","<ion-view title=\"'Pet Information'\"><ion-content has-header=\"true\" has-tabs=\"true\"><div class=\"list\"><a class=\"item item-thumbnail-left\" ng-repeat=\"pet in pets\" ng-href=\"#/tab/pet/{{pet.id}}\" ><img ng-src=\"{{pet.thumbnail}}\"><h3>{{pet.title}}</h3><p>{{pet.description}}</p></a></div></ion-content></ion-view>");
21+
$templateCache.put("templates/pet-detail.html","<ion-view title=\"pet.title\" right-buttons=\"rightButtons\"><ion-content has-header=\"true\" padding=\"true\"><p>{{ pet.description }}</p><p><a class=\"button button-small icon ion-arrow-left-b\" href=\"#/tab/pets\"> All Pets</a></p><button class=\"button button-small\" ng-click=\"clearViewHistories()\">clearViewHistories</button><button class=\"button button-small\" ng-click=\"historyConsole()\">Console Log</button><pre>{{ $viewHistory | json }}</pre></ion-content></ion-view>");
22+
23+
})
24+
.config(function($stateProvider, $urlRouterProvider) {
25+
$stateProvider
26+
.state('tab', {
27+
url: "/tab",
28+
abstract: true,
29+
templateUrl: "templates/tabs.html"
30+
})
31+
.state('tab.pet-index', {
32+
url: '/pets',
33+
views: {
34+
'pets-tab': {
35+
templateUrl: 'templates/pet-index.html',
36+
controller: 'PetIndexCtrl'
37+
}
38+
}
39+
})
40+
.state('tab.pet-detail', {
41+
url: '/pet/:petId',
42+
views: {
43+
'pets-tab': {
44+
templateUrl: 'templates/pet-detail.html',
45+
controller: 'PetDetailCtrl'
46+
}
47+
}
48+
})
49+
.state('tab.about', {
50+
url: '/about',
51+
views: {
52+
'about-tab': {
53+
templateUrl: 'templates/about.html'
54+
}
55+
}
56+
});
57+
$urlRouterProvider.otherwise('/tab/pets');
58+
59+
});
60+
61+
angular.module('starter.controllers', [])
62+
.controller('PetIndexCtrl', function($scope, PetService) {
63+
$scope.pets = PetService.all();
64+
})
65+
66+
.controller('PetDetailCtrl', function($scope, $stateParams, PetService, $ionicModal, $rootScope, $ionicViewService) {
67+
$scope.pet = PetService.get($stateParams.petId);
68+
$scope.historyConsole = function () {
69+
console.log($rootScope.$viewHistory);
70+
};
71+
$scope.clearViewHistories = function() {
72+
$ionicViewService.clearHistory();
73+
};
74+
})
75+
76+
angular.module('starter.services', [])
77+
.factory('PetService', function() {
78+
var pets = [
79+
{
80+
id: 0,
81+
title: 'Cats',
82+
description: 'Furry little creatures. Obsessed with plotting assassination, but never following through on it.',
83+
thumbnail: 'http://4.bp.blogspot.com/-MzZCzWI_6Xc/UIUQp1qPfzI/AAAAAAAAHpA/OTwHCJSWFAY/s1600/cats_animals_kittens_cat_kitten_cute_desktop_1680x1050_hd-wallpaper-753974.jpeg'
84+
},
85+
{
86+
id: 1,
87+
title: 'Dogs',
88+
description: 'Lovable. Loyal almost to a fault. Smarter than they let on.' ,
89+
thumbnail: 'http://davidfeldmanshow.com/wp-content/uploads/2014/01/dogs-wallpaper.jpg'
90+
},
91+
{
92+
id: 2,
93+
title: 'Turtles',
94+
description: 'Everyone likes turtles.' ,
95+
thumbnail: 'https://www.wildlifedepartment.com/wildlifemgmt/turtles/Common%20Map%20Turtle.jpg'
96+
},
97+
{
98+
id: 3,
99+
title: 'Sharks',
100+
description: 'An advanced pet. Needs millions of gallons of salt water. Will happily eat you.' ,
101+
thumbnail: 'http://upload.wikimedia.org/wikipedia/commons/e/ea/Prionace_glauca_1.jpg'
102+
},
103+
{
104+
id: 4,
105+
title: 'Cats',
106+
description: 'Furry little creatures. Obsessed with plotting assassination, but never following through on it.',
107+
thumbnail: 'http://4.bp.blogspot.com/-MzZCzWI_6Xc/UIUQp1qPfzI/AAAAAAAAHpA/OTwHCJSWFAY/s1600/cats_animals_kittens_cat_kitten_cute_desktop_1680x1050_hd-wallpaper-753974.jpeg'
108+
},
109+
{
110+
id: 5,
111+
title: 'Dogs',
112+
description: 'Lovable. Loyal almost to a fault. Smarter than they let on.' ,
113+
thumbnail: 'http://davidfeldmanshow.com/wp-content/uploads/2014/01/dogs-wallpaper.jpg'
114+
},
115+
{
116+
id: 6,
117+
title: 'Turtles',
118+
description: 'Everyone likes turtles.' ,
119+
thumbnail: 'https://www.wildlifedepartment.com/wildlifemgmt/turtles/Common%20Map%20Turtle.jpg'
120+
}
121+
];
122+
123+
return {
124+
all: function() {
125+
return pets;
126+
},
127+
get: function(petId) {
128+
// Simple index lookup
129+
return pets[petId];
130+
}
131+
}
132+
});
133+
134+
</script>
135+
</body>
136+
</html>

js/ext/angular/test/service/ionicViewService.unit.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('Ionic View Service', function() {
8888
expect(rootScope.$viewHistory.currentView.stateName).toEqual('home');
8989

9090
expect(rsp.viewId).not.toBeUndefined();
91-
expect(rootScope.$viewHistory.histories[rsp.viewId].viewId).toEqual(rsp.viewId);
91+
expect(rootScope.$viewHistory.views[rsp.viewId].viewId).toEqual(rsp.viewId);
9292
expect(viewService.getBackView()).toEqual(null);
9393
expect(viewService.getForwardView()).toEqual(null);
9494

@@ -684,7 +684,9 @@ describe('Ionic View Service', function() {
684684
expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(2);
685685
backView = viewService.getBackView();
686686
expect(backView).toBeDefined();
687+
expect( Object.keys(rootScope.$viewHistory.views).length ).toEqual(4);
687688
viewService.clearHistory();
689+
expect( Object.keys(rootScope.$viewHistory.views).length ).toEqual(1);
688690
expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(1);
689691
backView = viewService.getBackView();
690692
expect(backView).toEqual(null);

0 commit comments

Comments
 (0)