I am new to Javascript and backbone.js, so hopefully I missed something simple here. I am experimenting with some sample code that I found that should check for unsaved changes before allowing the user to go to another page. Here I created a JSfiddle:
http://jsfiddle.net/U43T5/4/
The code subscribes to the hashchange event as follows:
$(window).on("hashchange", router.hashChange);
And the function router.hashChangechecks the dirty flag to determine whether or not to allow navigation, for example:
hashChange: function (evt) {
if (this.cancelNavigate) {
evt.stopImmediatePropagation();
this.cancelNavigate = false;
return;
}
if (this.view && this.view.dirty) {
var dialog = confirm("You have unsaved changes. To stay on the page, press cancel. To discard changes and leave the page, press OK");
if (dialog == true) return;
else {
evt.stopImmediatePropagation();
this.cancelNavigate = true;
window.location.href = evt.originalEvent.oldURL;
}
}
},
The problem is that the code does not work, because it this.viewis undefined, so the 2nd if block is never entered.
, - ( this.view.dirty true, ). , , .