Document.ready is only triggered once by page load!
Thank you for your explanation, I have a much better idea of what you're trying to do.
If you still want to go down the object-oriented way, I suggests having a global "View" object and not overwriting it it, but calling a setter method on that object.
For example:
function View() {
this.state = {}
// the constructor goes here
}
View.prototype.setState = function(newState) {
this.state = newState;
}
To use it:
window.mainView = new View();
Then when you load a new partial view, you can do:
window.mainView.setState({
title: "blah",
content: "Lorem Ipsum..."
})
To read the state:
window.mainView.state
window.mainView.state.title
...
This will make your "global variable" a little more manageable, for example if the state is being overwritten and you don't know from where, the you can modify setState and find out from where it is being overwritten. You can also trigger an event when the state is being changed/overwritten, which I think will be useful, and you will want to refresh/do some things on the page following the state being changed.
Now that you have only 1 global object it's OK and easy to keep track of it. But if and when you have more, then it is better to organize your code into objects and methods which deal with the object itself, rather than to have functions write directly to global objects.
If prototypal Javascript is intimidating, you can just do:
window.mainView = {
state: {},
setState: setState = function(newState) {
// wait... we can't use "this" here, it won't be bound correctly
window.mainView.state = newState;
}
}
If your code gets more complicated, then you can extend this into the MVC pattern (Model, View, Controller) which is just a way of organizing and conceptualizing things. You'd have a Model object for working with data, a View object for dealing with the... view, and a Controller object for controlling and synchronizing all the other objects.
This approach also goes a little in the direction of React, but React does much more then that.
I hope your SPA will go well! I'm happy to help in the future.