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.
This commit is contained in:
David Baumgold
2013-06-24 15:21:27 -04:00
parent 0048c82092
commit 696a795c12
4 changed files with 25 additions and 19 deletions

View File

@@ -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()

View File

@@ -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", ->
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 = @model.get("chapters")
origLength = chapters.length chapters.at(0).set("name", "non-empty")
@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)
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", -> describe "CMS.Views.ListTextbooks", ->

View File

@@ -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
}; };
}, },

View File

@@ -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({
} 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;