diff --git a/cms/static/coffee/spec/models/textbook_spec.coffee b/cms/static/coffee/spec/models/textbook_spec.coffee index 4dd236b505..037fdf7312 100644 --- a/cms/static/coffee/spec/models/textbook_spec.coffee +++ b/cms/static/coffee/spec/models/textbook_spec.coffee @@ -14,11 +14,11 @@ describe "CMS.Models.Textbook", -> it "should not show chapters by default", -> expect(@model.get("showChapters")).toBeFalsy() - it "should have an empty ChapterSet by default", -> + it "should have a ChapterSet with one chapter by default", -> chapters = @model.get("chapters") expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet) - expect(chapters.isEmpty()).toBeTruthy() - expect(chapters.length).toEqual(0) + expect(chapters.length).toEqual(1) + expect(chapters.at(0).isEmpty()).toBeTruthy() it "should be empty by default", -> expect(@model.isEmpty()).toBeTruthy() diff --git a/cms/static/coffee/spec/views/textbook_spec.coffee b/cms/static/coffee/spec/views/textbook_spec.coffee index b890caa4aa..848afe9830 100644 --- a/cms/static/coffee/spec/views/textbook_spec.coffee +++ b/cms/static/coffee/spec/views/textbook_spec.coffee @@ -89,12 +89,6 @@ describe "CMS.Views.EditTextbook", -> @view.render() expect(@view.$("input[name=textbook-name]").val()).toEqual("Life Sciences") - it "should create an empty chapter when it is rendered, if there are no chapters", -> - expect(@model.get("chapters").length).toEqual(0) - @view.render() - expect(@model.get("chapters").length).toEqual(1) - expect(@model.get("chapters").last().isEmpty()).toBeTruthy() - it "should allow you to create new empty chapters", -> @view.render() numChapters = @model.get("chapters").length @@ -122,13 +116,23 @@ describe "CMS.Views.EditTextbook", -> expect(@collection.save).not.toHaveBeenCalled() expect(@collection.editing).toBeUndefined() - it "does not save empty chapters on cancel", -> + it "removes all empty chapters on cancel if the model has a non-empty chapter", -> chapters = @model.get("chapters") - origLength = chapters.length + chapters.at(0).set("name", "non-empty") @view.render() chapters.add([{}, {}, {}]) # add three empty chapters + expect(chapters.length).toEqual(4) @view.$(".action-cancel").click() - expect(chapters.length).toEqual(origLength) + expect(chapters.length).toEqual(1) + expect(chapters.at(0).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") + @view.render() + chapters.add([{}, {}, {}]) # add three empty chapters + expect(chapters.length).toEqual(4) + @view.$(".action-cancel").click() + expect(chapters.length).toEqual(1) describe "CMS.Views.ListTextbooks", -> diff --git a/cms/static/js/models/textbook.js b/cms/static/js/models/textbook.js index 42112a4d96..669f80c55b 100644 --- a/cms/static/js/models/textbook.js +++ b/cms/static/js/models/textbook.js @@ -2,7 +2,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ defaults: function() { return { name: "", - chapters: new CMS.Collections.ChapterSet(), + chapters: new CMS.Collections.ChapterSet([{}]), showChapters: false }; }, diff --git a/cms/static/js/views/textbook.js b/cms/static/js/views/textbook.js index 16ff5444c8..d09930c2a5 100644 --- a/cms/static/js/views/textbook.js +++ b/cms/static/js/views/textbook.js @@ -78,10 +78,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({ name: this.model.escape('name'), errors: null })); - var chapters = this.model.get('chapters'); - if (chapters.length === 0) { - chapters.add([{}]); - } this.addAll(); return this; }, @@ -153,8 +149,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({ } else { // remove empty chapters from textbook var chapters = this.model.get("chapters"); - chapters.remove(chapters.filter( - function(chapter) { return chapter.isEmpty(); })); + var emptyChapters = chapters.filter(function(chapter) { + return chapter.isEmpty(); }); + if (chapters.length === emptyChapters.length) { + // make sure that there's always at least one chapter remaining + // in the chapterset, even if it's empty + emptyChapters = _.tail(emptyChapters); + } + chapters.remove(emptyChapters); } textbooks.trigger('render'); return this;