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:
@@ -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()
|
||||
|
||||
@@ -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", ->
|
||||
|
||||
@@ -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
|
||||
};
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user