Commit cae5c324 by David Baumgold

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.
parent 27f23395
...@@ -14,10 +14,11 @@ describe "CMS.Models.Textbook", -> ...@@ -14,10 +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 a ChapterSet with one chapter by default", -> it "should have an empty ChapterSet by default", ->
chapters = @model.get("chapters") chapters = @model.get("chapters")
expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet) 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", -> it "should be empty by default", ->
expect(@model.isEmpty()).toBeTruthy() expect(@model.isEmpty()).toBeTruthy()
......
...@@ -89,10 +89,18 @@ describe "CMS.Views.TextbookEdit", -> ...@@ -89,10 +89,18 @@ describe "CMS.Views.TextbookEdit", ->
@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 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 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").length).toEqual(numChapters+1)
expect(@model.get("chapters").last().isEmpty()).toBeTruthy()
it "should save properly", -> it "should save properly", ->
@view.render() @view.render()
...@@ -104,6 +112,7 @@ describe "CMS.Views.TextbookEdit", -> ...@@ -104,6 +112,7 @@ describe "CMS.Views.TextbookEdit", ->
expect(@collection.save).toHaveBeenCalled() expect(@collection.save).toHaveBeenCalled()
it "does not save on cancel", -> it "does not save on cancel", ->
@model.get("chapters").add([{name: "a", asset_path: "b"}])
@view.render() @view.render()
@view.$("input[name=textbook-name]").val("starfish") @view.$("input[name=textbook-name]").val("starfish")
@view.$("input[name=chapter1-name]").val("foobar") @view.$("input[name=chapter1-name]").val("foobar")
...@@ -113,6 +122,14 @@ describe "CMS.Views.TextbookEdit", -> ...@@ -113,6 +122,14 @@ describe "CMS.Views.TextbookEdit", ->
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", ->
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", -> describe "CMS.Views.ListTextbooks", ->
noTextbooksTpl = readFixtures("no-textbooks.underscore") noTextbooksTpl = readFixtures("no-textbooks.underscore")
......
...@@ -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,6 +78,10 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -78,6 +78,10 @@ CMS.Views.TextbookEdit = 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;
}, },
...@@ -109,6 +113,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -109,6 +113,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
var that = this; var that = this;
_.each(this.$("li"), function(li, i) { _.each(this.$("li"), function(li, i) {
var chapter = that.model.get('chapters').at(i); var chapter = that.model.get('chapters').at(i);
if(!chapter) { return; }
chapter.set({ chapter.set({
"name": $(".chapter-name", li).val(), "name": $(".chapter-name", li).val(),
"asset_path": $(".chapter-asset-path", li).val() "asset_path": $(".chapter-asset-path", li).val()
...@@ -145,6 +150,11 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -145,6 +150,11 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
// if the textbook has no content, remove it from the collection // if the textbook has no content, remove it from the collection
if(this.model.isEmpty()) { if(this.model.isEmpty()) {
textbooks.remove(this.model); 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'); 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