javascript - How to deal with modified models when using Backbone.history -
i have simple controller+router below example purposes. question is: how deal router , backbone.history if have modified model?
let's fire router , controller. default route "" brings me function blue set background color blue. can click on button bring me function red sets background red , url /#red.
if click button, go "" url, background stay red. there way of getting previous state , not change url when dealing history?
i understand in function blue, set background blue , not this.model.get("background), asking in more complex cases, how previous state of this.model through 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"); } }); });
myapp.router.navigate("red"); - changes url doesn't trigger controller method fired. might tempted set {trigger:true} option, avoid that.
just call controller method route. have similar working example setup in answer here: how can show , hide regions in marionette.js?
the truth there several things messed in code example. like, shouldn't setting click handles in controller, that's view's job. controllers should abstract job, cleaning , rendering view. actual user interaction should handled in views.
Comments
Post a Comment