mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-06 22:29:44 +08:00
Basic NavController with Tests
This commit is contained in:
9
hacking/NavBar.js
Normal file
9
hacking/NavBar.js
Normal file
@ -0,0 +1,9 @@
|
||||
(function(window, document, ionic) {
|
||||
|
||||
NavBar = function() {
|
||||
};
|
||||
|
||||
NavBar.prototype = {
|
||||
};
|
||||
})(this, document, ion = this.ionic || {});
|
||||
|
||||
49
hacking/NavController.js
Normal file
49
hacking/NavController.js
Normal file
@ -0,0 +1,49 @@
|
||||
(function(window, document, ionic) {
|
||||
NavController = function(opts) {
|
||||
this.navBar = opts.navBar;
|
||||
this.controllers = opts.controllers || [];
|
||||
};
|
||||
|
||||
NavController.prototype = {
|
||||
getControllers: function() {
|
||||
return this.controllers;
|
||||
},
|
||||
getTopController: function() {
|
||||
return this.topController;
|
||||
},
|
||||
push: function(controller) {
|
||||
this.controllers.push(controller);
|
||||
|
||||
// Indicate we are switching controllers
|
||||
var shouldSwitch = this.switchingController && this.switchingController(controller) || true;
|
||||
|
||||
// Return if navigation cancelled
|
||||
if(shouldSwitch === false)
|
||||
return;
|
||||
|
||||
// Actually switch the active controllers
|
||||
|
||||
// Grab the top controller on the stack
|
||||
var next = this.controllers[this.controllers.length - 1];
|
||||
// Switch to it (TODO: Animate or such things here)
|
||||
this.topController = next;
|
||||
|
||||
return controller;
|
||||
},
|
||||
|
||||
|
||||
pop: function() {
|
||||
var next, last;
|
||||
|
||||
// Grab the controller behind the top one on the stack
|
||||
last = this.controllers.pop();
|
||||
|
||||
next = this.controllers[this.controllers.length - 1];
|
||||
|
||||
// Switch to it (TODO: Animate or such things here)
|
||||
this.topController = next;
|
||||
|
||||
return last;
|
||||
}
|
||||
};
|
||||
})(this, document, this.ionic || {});
|
||||
48
hacking/NavController.unit.js
Normal file
48
hacking/NavController.unit.js
Normal file
@ -0,0 +1,48 @@
|
||||
describe('NavController', function() {
|
||||
var ctrl;
|
||||
|
||||
beforeEach(function() {
|
||||
ctrl = new NavController({
|
||||
navBar: new NavBar()
|
||||
});
|
||||
});
|
||||
|
||||
it('Should load controllers', function() {
|
||||
ctrl = new NavController({
|
||||
controllers: [{}]
|
||||
});
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
});
|
||||
|
||||
it('Should push controller', function() {
|
||||
ctrl.push({
|
||||
title: 'Page 1'
|
||||
});
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
ctrl.push({
|
||||
title: 'Page 2'
|
||||
});
|
||||
expect(ctrl.getControllers().length).toEqual(2);
|
||||
var last = ctrl.pop();
|
||||
expect(ctrl.getControllers().length).toEqual(1);
|
||||
expect(last.title).toEqual('Page 2');
|
||||
});
|
||||
|
||||
it('Should change top view controller', function() {
|
||||
expect(ctrl.getTopController()).toBe(undefined);
|
||||
var c1 = {
|
||||
title: 'Page 1'
|
||||
};
|
||||
var c2 = {
|
||||
title: 'Page 2'
|
||||
};
|
||||
ctrl.push(c1);
|
||||
expect(ctrl.getTopController()).toEqual(c1);
|
||||
ctrl.push(c2);
|
||||
expect(ctrl.getTopController()).toEqual(c2);
|
||||
ctrl.pop();
|
||||
expect(ctrl.getTopController()).toEqual(c1);
|
||||
ctrl.pop();
|
||||
expect(ctrl.getTopController()).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
@ -186,4 +186,4 @@ TabBar.prototype = {
|
||||
}
|
||||
};
|
||||
|
||||
})(this, document, ion = this.ionic || {});
|
||||
})(this, document, this.ionic || {});
|
||||
|
||||
Reference in New Issue
Block a user