I have this fairly simple controller
+router
below for example purposes. My question is: How do I deal with the router and Backbone.history
if I have modified a certain model?
Let's say that I fire up a route
r and a controller
. The default route ""
brings me to the function blue
where I set the background to the color blue. I can click on a button that will bring me to the function red
where it sets the background red and the URL to /#red
.
If I click the back button, I will go back to my ""
url, but the background will stay red. Is there a way of getting the previous state and not only change the URL when dealing with the history?
I understand that in function blue
, I could just set the background to be blue and not to this.model.get("background)
, but I am asking in more complex cases, how to get the previous state of this.model
through the Backbone.history
.
MyApp.module('Main', function (Main, MyApp, Backbone, Marionette, $, _){
Main.Router = Marionette.AppRouter.extend({
appRoutes: {
"": "blue",
"red": "red"
}
});
Main.Controller = Marionette.Controller.extend({
start: function() {
console.log("MyApp Controller start...");
this.model = new Backbone.Model({background: "blue"});
Backbone.history.start()
},
blue: function() {
$("body").css("background-color", this.model.get("background"));
$("#button").click(function() {
//MyApp.router.navigate("red", {trigger: true});
MyApp.controller.red();
});
},
red: function() {
this.model.set({background: "red"});
$("body").css("background-color", this.model.get("background"));
MyApp.router.navigate("red");
}
});
});