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.
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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,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;
|
||||
|
||||
Reference in New Issue
Block a user