Here is an example of adding a new data source to an XPage at runtime:
var viewDataSource = new com.ibm.xsp.model.domino.DominoViewData();
viewDataSource.setViewName("($All)");
viewDataSource.setVar("allDocuments");
view.addData(viewDataSource);
In order for this technique to be of use, however, other components must reference it by its var (in the above example, allDocuments). Unless you're also injecting all of those components at runtime as well, they're referring to a data source that already exists. So here's a function that should do what you're attempting (read: I haven't tested it, but it works in my mind).
function replaceViewData(oldDataSource, newViewName) {
var viewDataSource = new com.ibm.xsp.model.domino.DominoViewData();
viewDataSource.setViewName(newViewName);
viewDataSource.setVar(oldDataSource.getVar());
view.getData().remove(oldDataSource);
view.addData(viewDataSource);
}
You would pass a handle on the existing data source and the name of the view you want to switch to. Theoretically, you could just call setViewName() on the original data source, and - again - it should switch to using the view matching the name you pass... but I'd be far more confident that removing the data source entirely and injecting a new one would work.
Also, keep in mind that data sources can be bound to a panel as well as to the entire ViewRoot, so if your data source is in a panel, you'd need to reference that panel when removing the old data source and adding the new, instead of the ViewRoot.
One final word of caution: if you're using a view panel - or any other components that make assumptions about the name or sequence of columns in the view - you will encounter problems if you replace an existing view data source with one whose columns cause those assumptions to no longer be accurate. For example, if a view panel includes a column that exists in the original view but not the one you're replacing it with, the entire page will 500. So you may need to inject / remove / replace additional components as well when swapping out a data source. This would apply equally when changing the form to which a document data source is bound.