Commit 70a01177 by David Baumgold

Show a warning if the user tries to leave the page with unsaved textbook changes

parent fedbf458
...@@ -26,6 +26,18 @@ describe "CMS.Models.Textbook", -> ...@@ -26,6 +26,18 @@ describe "CMS.Models.Textbook", ->
it "should have a URL set", -> it "should have a URL set", ->
expect(_.result(@model, "url")).toBeTruthy() expect(_.result(@model, "url")).toBeTruthy()
it "should not be dirty by default", ->
expect(@model.isDirty()).toBeFalsy()
it "should be dirty after it's been changed", ->
@model.set("name", "foobar")
expect(@model.isDirty()).toBeTruthy()
it "should not be dirty after calling setOriginalAttributes", ->
@model.set("name", "foobar")
@model.setOriginalAttributes()
expect(@model.isDirty()).toBeFalsy()
describe "CMS.Models.Textbook input/output", -> describe "CMS.Models.Textbook input/output", ->
# replace with Backbone.Assocations.deepAttributes when # replace with Backbone.Assocations.deepAttributes when
......
...@@ -23,6 +23,9 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -23,6 +23,9 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
reset: function() { reset: function() {
this.set(this._originalAttributes, {parse: true}); this.set(this._originalAttributes, {parse: true});
}, },
isDirty: function() {
return !_.isEqual(this._originalAttributes, this.parse(this.toJSON()));
},
isEmpty: function() { isEmpty: function() {
return !this.get('name') && this.get('chapters').isEmpty(); return !this.get('name') && this.get('chapters').isEmpty();
}, },
......
...@@ -30,6 +30,12 @@ $(function() { ...@@ -30,6 +30,12 @@ $(function() {
$(".nav-actions .new-button").click(function(e) { $(".nav-actions .new-button").click(function(e) {
tbView.addOne(e); tbView.addOne(e);
}) })
$(window).on("beforeunload", function() {
var dirty = textbooks.find(function(textbook) { return textbook.isDirty(); });
if(dirty) {
return "You have unsaved changes. Do you really want to leave this page?";
}
})
}) })
</script> </script>
</%block> </%block>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment