You could keep a hold of the current view and then have a function that handles the add/remove actions.
var currentView = null;
function replaceView(view) {
if(currentView) {
window.remove(currentView);
currentView = null;
}
currentView = view;
window.add(currentView)
}
Then when you find out which view to show you just call the function.
replaceView( viewToShow );