From cae5c324a60f44808c5052e44cdc720a87f2d9fb Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Fri, 21 Jun 2013 15:57:42 -0400 Subject: [PATCH] modifications to Backbone textbook structure Textbooks now have an empty chapterset by default, instead of one with one empty chapter. The TextbookEdit view dynamically adds an empty chapter if the chapterset is empty; the close button removes all empty chapters from the chapterset. --- .../coffee/spec/models/textbook_spec.coffee | 5 +++-- .../coffee/spec/views/textbook_spec.coffee | 21 +++++++++++++++++-- cms/static/js/models/textbook.js | 2 +- cms/static/js/views/textbook.js | 10 +++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cms/static/coffee/spec/models/textbook_spec.coffee b/cms/static/coffee/spec/models/textbook_spec.coffee index 7153122a8c..2a3cc20e97 100644 --- a/cms/static/coffee/spec/models/textbook_spec.coffee +++ b/cms/static/coffee/spec/models/textbook_spec.coffee @@ -14,10 +14,11 @@ describe "CMS.Models.Textbook", -> it "should not show chapters by default", -> expect(@model.get("showChapters")).toBeFalsy() - it "should have a ChapterSet with one chapter by default", -> + it "should have an empty ChapterSet by default", -> chapters = @model.get("chapters") expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet) - expect(chapters.length).toEqual(1) + expect(chapters.isEmpty()).toBeTruthy() + expect(chapters.length).toEqual(0) 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 d3086ea9b0..e50f8f46a0 100644 --- a/cms/static/coffee/spec/views/textbook_spec.coffee +++ b/cms/static/coffee/spec/views/textbook_spec.coffee @@ -89,10 +89,18 @@ describe "CMS.Views.TextbookEdit", -> @view.render() expect(@view.$("input[name=textbook-name]").val()).toEqual("Life Sciences") - it "should allow you to create new chapters", -> + 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 - @view.render().$(".action-add-chapter").click() + @view.$(".action-add-chapter").click() expect(@model.get("chapters").length).toEqual(numChapters+1) + expect(@model.get("chapters").last().isEmpty()).toBeTruthy() it "should save properly", -> @view.render() @@ -104,6 +112,7 @@ describe "CMS.Views.TextbookEdit", -> expect(@collection.save).toHaveBeenCalled() it "does not save on cancel", -> + @model.get("chapters").add([{name: "a", asset_path: "b"}]) @view.render() @view.$("input[name=textbook-name]").val("starfish") @view.$("input[name=chapter1-name]").val("foobar") @@ -113,6 +122,14 @@ describe "CMS.Views.TextbookEdit", -> expect(@collection.save).not.toHaveBeenCalled() expect(@collection.editing).toBeUndefined() + it "does not save empty chapters on cancel", -> + chapters = @model.get("chapters") + origLength = chapters.length + @view.render() + chapters.add([{}, {}, {}]) # add three empty chapters + @view.$(".action-cancel").click() + expect(chapters.length).toEqual(origLength) + describe "CMS.Views.ListTextbooks", -> noTextbooksTpl = readFixtures("no-textbooks.underscore") diff --git a/cms/static/js/models/textbook.js b/cms/static/js/models/textbook.js index 4870b003a4..1b7b53f59d 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 dd2816254d..0394640e5d 100644 --- a/cms/static/js/views/textbook.js +++ b/cms/static/js/views/textbook.js @@ -78,6 +78,10 @@ CMS.Views.TextbookEdit = 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; }, @@ -109,6 +113,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ var that = this; _.each(this.$("li"), function(li, i) { var chapter = that.model.get('chapters').at(i); + if(!chapter) { return; } chapter.set({ "name": $(".chapter-name", li).val(), "asset_path": $(".chapter-asset-path", li).val() @@ -145,6 +150,11 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ // if the textbook has no content, remove it from the collection if(this.model.isEmpty()) { textbooks.remove(this.model); + } else { + // remove empty chapters from textbook + var chapters = this.model.get("chapters"); + chapters.remove(chapters.filter( + function(chapter) { return chapter.isEmpty(); })); } textbooks.trigger('render'); return this;