Commit 696a795c by David Baumgold

Don't remove all chapters on closing EditTextbook view

Previously, the code would dynamically add a chapter on render() if the textbook
had no chapters, and would remove all empty chapters when the edit view was closed.
Now, the render method doesn't modify the model at all anymore (yay!) and when the
edit view is closed, remove all empty chapters but be sure to leave at least one
left.
parent 0048c820
...@@ -14,11 +14,11 @@ describe "CMS.Models.Textbook", -> ...@@ -14,11 +14,11 @@ describe "CMS.Models.Textbook", ->
it "should not show chapters by default", -> it "should not show chapters by default", ->
expect(@model.get("showChapters")).toBeFalsy() 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") chapters = @model.get("chapters")
expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet) expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet)
expect(chapters.isEmpty()).toBeTruthy() expect(chapters.length).toEqual(1)
expect(chapters.length).toEqual(0) expect(chapters.at(0).isEmpty()).toBeTruthy()
it "should be empty by default", -> it "should be empty by default", ->
expect(@model.isEmpty()).toBeTruthy() expect(@model.isEmpty()).toBeTruthy()
......
...@@ -89,12 +89,6 @@ describe "CMS.Views.EditTextbook", -> ...@@ -89,12 +89,6 @@ describe "CMS.Views.EditTextbook", ->
@view.render() @view.render()
expect(@view.$("input[name=textbook-name]").val()).toEqual("Life Sciences") 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", -> it "should allow you to create new empty chapters", ->
@view.render() @view.render()
numChapters = @model.get("chapters").length numChapters = @model.get("chapters").length
...@@ -122,13 +116,23 @@ describe "CMS.Views.EditTextbook", -> ...@@ -122,13 +116,23 @@ describe "CMS.Views.EditTextbook", ->
expect(@collection.save).not.toHaveBeenCalled() expect(@collection.save).not.toHaveBeenCalled()
expect(@collection.editing).toBeUndefined() 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")
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(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") chapters = @model.get("chapters")
origLength = chapters.length
@view.render() @view.render()
chapters.add([{}, {}, {}]) # add three empty chapters chapters.add([{}, {}, {}]) # add three empty chapters
expect(chapters.length).toEqual(4)
@view.$(".action-cancel").click() @view.$(".action-cancel").click()
expect(chapters.length).toEqual(origLength) expect(chapters.length).toEqual(1)
describe "CMS.Views.ListTextbooks", -> describe "CMS.Views.ListTextbooks", ->
......
...@@ -2,7 +2,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -2,7 +2,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
defaults: function() { defaults: function() {
return { return {
name: "", name: "",
chapters: new CMS.Collections.ChapterSet(), chapters: new CMS.Collections.ChapterSet([{}]),
showChapters: false showChapters: false
}; };
}, },
......
...@@ -78,10 +78,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -78,10 +78,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({
name: this.model.escape('name'), name: this.model.escape('name'),
errors: null errors: null
})); }));
var chapters = this.model.get('chapters');
if (chapters.length === 0) {
chapters.add([{}]);
}
this.addAll(); this.addAll();
return this; return this;
}, },
...@@ -153,8 +149,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -153,8 +149,14 @@ CMS.Views.EditTextbook = Backbone.View.extend({
} else { } else {
// remove empty chapters from textbook // remove empty chapters from textbook
var chapters = this.model.get("chapters"); var chapters = this.model.get("chapters");
chapters.remove(chapters.filter( var emptyChapters = chapters.filter(function(chapter) {
function(chapter) { return chapter.isEmpty(); })); 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'); textbooks.trigger('render');
return this; return this;
......
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