From fedbf4585b7ea010d75682e4fb6a8c432affdc4d Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 25 Jun 2013 17:20:59 -0400 Subject: [PATCH] Prevent saving when textbook is in invalid state --- .../coffee/spec/views/textbook_spec.coffee | 32 ++++++++++++++++--- cms/static/js/models/textbook.js | 2 +- cms/static/js/views/textbook.js | 6 ++-- cms/templates/js/edit-textbook.underscore | 4 +-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cms/static/coffee/spec/views/textbook_spec.coffee b/cms/static/coffee/spec/views/textbook_spec.coffee index b02e904522..d6caf8d97d 100644 --- a/cms/static/coffee/spec/views/textbook_spec.coffee +++ b/cms/static/coffee/spec/views/textbook_spec.coffee @@ -96,31 +96,53 @@ describe "CMS.Views.EditTextbook", -> it "should save properly", -> @view.render() @view.$("input[name=textbook-name]").val("starfish") - @view.$("input[name=chapter1-name]").val("foobar") + @view.$("input[name=chapter1-asset-path]").val("foobar") @view.$("form").submit() expect(@model.get("name")).toEqual("starfish") - expect(@model.get("chapters").at(0).get("name")).toEqual("foobar") + expect(@model.get("chapters").at(0).get("asset_path")).toEqual("foobar") expect(@model.save).toHaveBeenCalled() + it "should not save on invalid", -> + @view.render() + @view.$("input[name=textbook-name]").val("") + @view.$("input[name=chapter1-asset-path]").val("foobar.pdf") + @view.$("form").submit() + expect(@model.validationError).toBeTruthy() + expect(@model.save).not.toHaveBeenCalled() + it "does not save on cancel", -> @model.get("chapters").add([{name: "a", asset_path: "b"}]) @view.render() @view.$("input[name=textbook-name]").val("starfish") - @view.$("input[name=chapter1-name]").val("foobar") + @view.$("input[name=chapter1-asset-path]").val("foobar.pdf") @view.$(".action-cancel").click() expect(@model.get("name")).not.toEqual("starfish") - expect(@model.get("chapters").at(0).get("name")).not.toEqual("foobar") + expect(@model.get("chapters").first().get("asset_path")).not.toEqual("foobar") expect(@model.save).not.toHaveBeenCalled() + it "should be possible to correct validation errors", -> + @view.render() + @view.$("input[name=textbook-name]").val("") + @view.$("input[name=chapter1-asset-path]").val("foobar.pdf") + @view.$("form").submit() + expect(@model.validationError).toBeTruthy() + expect(@model.save).not.toHaveBeenCalled() + @view.$("input[name=textbook-name]").val("starfish") + @view.$("input[name=chapter1-name]").val("foobar") + @view.$("form").submit() + expect(@model.validationError).toBeFalsy() + expect(@model.save).toHaveBeenCalled() + it "removes all empty chapters on cancel if the model has a non-empty chapter", -> chapters = @model.get("chapters") chapters.at(0).set("name", "non-empty") + @model.setOriginalAttributes() @view.render() chapters.add([{}, {}, {}]) # add three empty chapters expect(chapters.length).toEqual(4) @view.$(".action-cancel").click() expect(chapters.length).toEqual(1) - expect(chapters.at(0).get('name')).toEqual("non-empty") + expect(chapters.first().get('name')).toEqual("non-empty") it "removes all empty chapters on cancel except one if the model has no non-empty chapters", -> chapters = @model.get("chapters") diff --git a/cms/static/js/models/textbook.js b/cms/static/js/models/textbook.js index be87d0b67b..83ac45b88d 100644 --- a/cms/static/js/models/textbook.js +++ b/cms/static/js/models/textbook.js @@ -21,7 +21,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ this._originalAttributes = this.parse(this.toJSON()); }, reset: function() { - this.set(this._originalAttributes); + this.set(this._originalAttributes, {parse: true}); }, isEmpty: function() { return !this.get('name') && this.get('chapters').isEmpty(); diff --git a/cms/static/js/views/textbook.js b/cms/static/js/views/textbook.js index a27d32f3db..168e815967 100644 --- a/cms/static/js/views/textbook.js +++ b/cms/static/js/views/textbook.js @@ -65,6 +65,7 @@ CMS.Views.ShowTextbook = Backbone.View.extend({ CMS.Views.EditTextbook = Backbone.View.extend({ initialize: function() { this.template = _.template($("#edit-textbook-tpl").text()); + this.listenTo(this.model, "invalid", this.render); var chapters = this.model.get('chapters'); this.listenTo(chapters, "add", this.addOne); this.listenTo(chapters, "reset", this.addAll); @@ -75,7 +76,7 @@ CMS.Views.EditTextbook = Backbone.View.extend({ render: function() { this.$el.html(this.template({ name: this.model.escape('name'), - errors: null + error: this.model.validationError })); this.addAll(); return this; @@ -119,13 +120,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({ setAndClose: function(e) { if(e && e.preventDefault) { e.preventDefault(); } this.setValues(); + if(!this.model.isValid()) { return; } var saving = new CMS.Views.Notification.Saving({ title: gettext("Saving…") }); var that = this; this.model.save({}, { success: function() { - that.setOriginalAttributes(); + that.model.setOriginalAttributes(); that.close(); }, complete: function() { diff --git a/cms/templates/js/edit-textbook.underscore b/cms/templates/js/edit-textbook.underscore index bb1dce0b9b..a03681dbb8 100644 --- a/cms/templates/js/edit-textbook.underscore +++ b/cms/templates/js/edit-textbook.underscore @@ -1,8 +1,8 @@
- <% if (errors) { %> + <% if (error) { %>
- <%= errors %> + <%= error %>
<% } %>