Commit fedbf458 by David Baumgold

Prevent saving when textbook is in invalid state

parent 74b9703d
...@@ -96,31 +96,53 @@ describe "CMS.Views.EditTextbook", -> ...@@ -96,31 +96,53 @@ describe "CMS.Views.EditTextbook", ->
it "should save properly", -> it "should save properly", ->
@view.render() @view.render()
@view.$("input[name=textbook-name]").val("starfish") @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() @view.$("form").submit()
expect(@model.get("name")).toEqual("starfish") 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() 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", -> it "does not save on cancel", ->
@model.get("chapters").add([{name: "a", asset_path: "b"}]) @model.get("chapters").add([{name: "a", asset_path: "b"}])
@view.render() @view.render()
@view.$("input[name=textbook-name]").val("starfish") @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() @view.$(".action-cancel").click()
expect(@model.get("name")).not.toEqual("starfish") 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() 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", -> it "removes all empty chapters on cancel if the model has a non-empty chapter", ->
chapters = @model.get("chapters") chapters = @model.get("chapters")
chapters.at(0).set("name", "non-empty") chapters.at(0).set("name", "non-empty")
@model.setOriginalAttributes()
@view.render() @view.render()
chapters.add([{}, {}, {}]) # add three empty chapters chapters.add([{}, {}, {}]) # add three empty chapters
expect(chapters.length).toEqual(4) expect(chapters.length).toEqual(4)
@view.$(".action-cancel").click() @view.$(".action-cancel").click()
expect(chapters.length).toEqual(1) 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", -> it "removes all empty chapters on cancel except one if the model has no non-empty chapters", ->
chapters = @model.get("chapters") chapters = @model.get("chapters")
......
...@@ -21,7 +21,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -21,7 +21,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
this._originalAttributes = this.parse(this.toJSON()); this._originalAttributes = this.parse(this.toJSON());
}, },
reset: function() { reset: function() {
this.set(this._originalAttributes); this.set(this._originalAttributes, {parse: true});
}, },
isEmpty: function() { isEmpty: function() {
return !this.get('name') && this.get('chapters').isEmpty(); return !this.get('name') && this.get('chapters').isEmpty();
......
...@@ -65,6 +65,7 @@ CMS.Views.ShowTextbook = Backbone.View.extend({ ...@@ -65,6 +65,7 @@ CMS.Views.ShowTextbook = Backbone.View.extend({
CMS.Views.EditTextbook = Backbone.View.extend({ CMS.Views.EditTextbook = Backbone.View.extend({
initialize: function() { initialize: function() {
this.template = _.template($("#edit-textbook-tpl").text()); this.template = _.template($("#edit-textbook-tpl").text());
this.listenTo(this.model, "invalid", this.render);
var chapters = this.model.get('chapters'); var chapters = this.model.get('chapters');
this.listenTo(chapters, "add", this.addOne); this.listenTo(chapters, "add", this.addOne);
this.listenTo(chapters, "reset", this.addAll); this.listenTo(chapters, "reset", this.addAll);
...@@ -75,7 +76,7 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -75,7 +76,7 @@ CMS.Views.EditTextbook = Backbone.View.extend({
render: function() { render: function() {
this.$el.html(this.template({ this.$el.html(this.template({
name: this.model.escape('name'), name: this.model.escape('name'),
errors: null error: this.model.validationError
})); }));
this.addAll(); this.addAll();
return this; return this;
...@@ -119,13 +120,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -119,13 +120,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({
setAndClose: function(e) { setAndClose: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
this.setValues(); this.setValues();
if(!this.model.isValid()) { return; }
var saving = new CMS.Views.Notification.Saving({ var saving = new CMS.Views.Notification.Saving({
title: gettext("Saving…") title: gettext("Saving…")
}); });
var that = this; var that = this;
this.model.save({}, { this.model.save({}, {
success: function() { success: function() {
that.setOriginalAttributes(); that.model.setOriginalAttributes();
that.close(); that.close();
}, },
complete: function() { complete: function() {
......
<form class="edit-textbook" id="edit_textbook_form"> <form class="edit-textbook" id="edit_textbook_form">
<div class="wrapper-form"> <div class="wrapper-form">
<% if (errors) { %> <% if (error) { %>
<div id="edit_textbook_error" class="message message-status message-status error" name="edit_textbook_error"> <div id="edit_textbook_error" class="message message-status message-status error" name="edit_textbook_error">
<%= errors %> <%= error %>
</div> </div>
<% } %> <% } %>
......
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