Basic NavController working with demo

This commit is contained in:
Max Lynch
2013-09-16 21:56:22 -05:00
parent 19d2aa5432
commit 0f5ce5b76e
5 changed files with 208 additions and 18 deletions

View File

@ -1,7 +1,16 @@
(function(window, document, ionic) {
NavController = function(opts) {
var _this = this;
this.navBar = opts.navBar;
this.content = opts.content;
this.controllers = opts.controllers || [];
// TODO: Is this the best way?
this.navBar.shouldGoBack = function() {
_this.pop();
}
};
NavController.prototype = {
@ -12,6 +21,8 @@
return this.topController;
},
push: function(controller) {
var last = this.topController;
this.controllers.push(controller);
// Indicate we are switching controllers
@ -23,27 +34,57 @@
// Actually switch the active controllers
// Remove the old one
last && last.detach();
// Grab the top controller on the stack
var next = this.controllers[this.controllers.length - 1];
this.content.el.appendChild(next.el);
// Switch to it (TODO: Animate or such things here)
this.topController = next;
this._updateNavBar();
return controller;
},
pop: function() {
var next, last;
// Make sure we keep one on the stack at all times
if(this.controllers.length < 2) {
return;
}
// Grab the controller behind the top one on the stack
last = this.controllers.pop();
// Remove the old one
last && last.detach();
next = this.controllers[this.controllers.length - 1];
this.content.el.appendChild(next.el);
// Switch to it (TODO: Animate or such things here)
this.topController = next;
this._updateNavBar();
return last;
}
},
_updateNavBar: function() {
this.navBar.setTitle(this.topController.title);
if(this.controllers.length > 1) {
this.navBar.showBackButton(true);
} else {
this.navBar.showBackButton(false);
}
},
};
})(this, document, this.ionic || {});