Commit dc5123c8 by David Baumgold

Allow multiple textbook edit forms on the page simultaneously

parent 404c36b8
...@@ -53,6 +53,7 @@ describe "CMS.Models.Textbook input/output", -> ...@@ -53,6 +53,7 @@ describe "CMS.Models.Textbook input/output", ->
clientModelSpec = { clientModelSpec = {
"name": "My Textbook", "name": "My Textbook",
"showChapters": false, "showChapters": false,
"editing": false,
"chapters": [{ "chapters": [{
"name": "Chapter 1", "name": "Chapter 1",
"asset_path": "/ch1.pdf", "asset_path": "/ch1.pdf",
...@@ -85,11 +86,6 @@ describe "CMS.Collections.TextbookSet", -> ...@@ -85,11 +86,6 @@ describe "CMS.Collections.TextbookSet", ->
@collection.save() @collection.save()
expect(@collection.sync).toHaveBeenCalledWith("update", @collection, undefined) expect(@collection.sync).toHaveBeenCalledWith("update", @collection, undefined)
it "should respond to editOne events", ->
model = new CMS.Models.Textbook()
@collection.trigger('editOne', model)
expect(@collection.editing).toEqual(model)
describe "CMS.Models.Chapter", -> describe "CMS.Models.Chapter", ->
beforeEach -> beforeEach ->
......
...@@ -29,11 +29,9 @@ describe "CMS.Views.ShowTextbook", -> ...@@ -29,11 +29,9 @@ describe "CMS.Views.ShowTextbook", ->
@view.render() @view.render()
expect(@view.$el).toContainText("Life Sciences") expect(@view.$el).toContainText("Life Sciences")
it "should trigger an editOne event on the collection when the edit button is clicked", -> it "should set the 'editing' property on the model when the edit button is clicked", ->
spyOn(@collection, "trigger").andCallThrough()
@view.render().$(".edit").click() @view.render().$(".edit").click()
expect(@collection.trigger).toHaveBeenCalledWith("editOne", @model) expect(@model.get("editing")).toBeTruthy()
expect(@collection.editing).toEqual(@model)
it "should pop a delete confirmation when the delete button is clicked", -> it "should pop a delete confirmation when the delete button is clicked", ->
promptSpies = spyOnConstructor(CMS.Views.Prompt, "Warning", ["show", "hide"]) promptSpies = spyOnConstructor(CMS.Views.Prompt, "Warning", ["show", "hide"])
...@@ -77,11 +75,10 @@ describe "CMS.Views.EditTextbook", -> ...@@ -77,11 +75,10 @@ describe "CMS.Views.EditTextbook", ->
appendSetFixtures($("<script>", {id: "system-feedback-tpl", type: "text/template"}).text(feedbackTpl)) appendSetFixtures($("<script>", {id: "system-feedback-tpl", type: "text/template"}).text(feedbackTpl))
appendSetFixtures(sandbox({id: "page-notification"})) appendSetFixtures(sandbox({id: "page-notification"}))
appendSetFixtures(sandbox({id: "page-prompt"})) appendSetFixtures(sandbox({id: "page-prompt"}))
@model = new CMS.Models.Textbook({name: "Life Sciences"}) @model = new CMS.Models.Textbook({name: "Life Sciences", editing: true})
@collection = new CMS.Collections.TextbookSet() @collection = new CMS.Collections.TextbookSet()
spyOn(@collection, 'save') spyOn(@collection, 'save')
@collection.add(@model) @collection.add(@model)
@collection.editing = @model
@view = new CMS.Views.EditTextbook({model: @model}) @view = new CMS.Views.EditTextbook({model: @model})
spyOn(@view, 'render').andCallThrough() spyOn(@view, 'render').andCallThrough()
...@@ -114,7 +111,6 @@ describe "CMS.Views.EditTextbook", -> ...@@ -114,7 +111,6 @@ describe "CMS.Views.EditTextbook", ->
expect(@model.get("name")).not.toEqual("starfish") expect(@model.get("name")).not.toEqual("starfish")
expect(@model.get("chapters").at(0).get("name")).not.toEqual("foobar") expect(@model.get("chapters").at(0).get("name")).not.toEqual("foobar")
expect(@collection.save).not.toHaveBeenCalled() expect(@collection.save).not.toHaveBeenCalled()
expect(@collection.editing).toBeUndefined()
it "removes all empty chapters on cancel if the model has a non-empty chapter", -> it "removes all empty chapters on cancel if the model has a non-empty chapter", ->
chapters = @model.get("chapters") chapters = @model.get("chapters")
...@@ -178,11 +174,11 @@ describe "CMS.Views.ListTextbooks", -> ...@@ -178,11 +174,11 @@ describe "CMS.Views.ListTextbooks", ->
expect(@editSpies.constructor).not.toHaveBeenCalled() expect(@editSpies.constructor).not.toHaveBeenCalled()
it "should render an EditTextbook view for a textbook being edited", -> it "should render an EditTextbook view for a textbook being edited", ->
# add three empty textbooks to the collection # add three empty textbooks to the collection: the first and third
@collection.add([{}, {}, {}]) # should be shown, and the second should be edited
# mark the second one as being edited @collection.add([{editing: false}, {editing: true}, {editing: false}])
editing = @collection.at(1) editing = @collection.at(1)
@collection.trigger('editOne', editing) expect(editing.get("editing")).toBeTruthy()
# reset spies # reset spies
@showSpies.constructor.reset() @showSpies.constructor.reset()
@editSpies.constructor.reset() @editSpies.constructor.reset()
...@@ -204,10 +200,6 @@ describe "CMS.Views.ListTextbooks", -> ...@@ -204,10 +200,6 @@ describe "CMS.Views.ListTextbooks", ->
@view.$(".new-button").click() @view.$(".new-button").click()
expect(@collection.length).toEqual(1) expect(@collection.length).toEqual(1)
expect(@collection.editing).toBeDefined()
editing = @collection.editing
expect(editing).toEqual(@collection.at(0))
expect(@editSpies.constructor).toHaveBeenCalledWith({model: editing})
expect(@view.$el).toContain(@editSpies.$el) expect(@view.$el).toContain(@editSpies.$el)
expect(@view.$el).not.toContain(@showSpies.$el) expect(@view.$el).not.toContain(@showSpies.$el)
......
...@@ -3,7 +3,8 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -3,7 +3,8 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
return { return {
name: "", name: "",
chapters: new CMS.Collections.ChapterSet([{}]), chapters: new CMS.Collections.ChapterSet([{}]),
showChapters: false showChapters: false,
editing: false
}; };
}, },
relations: [{ relations: [{
...@@ -40,12 +41,6 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -40,12 +41,6 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
CMS.Collections.TextbookSet = Backbone.Collection.extend({ CMS.Collections.TextbookSet = Backbone.Collection.extend({
model: CMS.Models.Textbook, model: CMS.Models.Textbook,
url: function() { return CMS.URL.TEXTBOOK; }, url: function() { return CMS.URL.TEXTBOOK; },
initialize: function() {
this.listenTo(this, "editOne", this.editOne);
},
editOne: function(textbook) {
this.editing = textbook;
},
save: function(options) { save: function(options) {
return this.sync('update', this, options); return this.sync('update', this, options);
} }
......
...@@ -17,7 +17,7 @@ CMS.Views.ShowTextbook = Backbone.View.extend({ ...@@ -17,7 +17,7 @@ CMS.Views.ShowTextbook = Backbone.View.extend({
}, },
editTextbook: function(e) { editTextbook: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
this.model.collection.trigger("editOne", this.model); this.model.set("editing", true);
}, },
confirmDelete: function(e) { confirmDelete: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
...@@ -69,7 +69,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -69,7 +69,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({
this.listenTo(chapters, "add", this.addOne); this.listenTo(chapters, "add", this.addOne);
this.listenTo(chapters, "reset", this.addAll); this.listenTo(chapters, "reset", this.addAll);
this.listenTo(chapters, "all", this.render); this.listenTo(chapters, "all", this.render);
this.listenTo(this.model.collection, "editOne", this.remove);
}, },
tagName: "section", tagName: "section",
className: "textbook", className: "textbook",
...@@ -141,7 +140,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -141,7 +140,6 @@ CMS.Views.EditTextbook = Backbone.View.extend({
}, },
close: function() { close: function() {
var textbooks = this.model.collection; var textbooks = this.model.collection;
delete textbooks.editing;
this.remove(); this.remove();
// 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()) {
...@@ -157,8 +155,9 @@ CMS.Views.EditTextbook = Backbone.View.extend({ ...@@ -157,8 +155,9 @@ CMS.Views.EditTextbook = Backbone.View.extend({
emptyChapters = _.tail(emptyChapters); emptyChapters = _.tail(emptyChapters);
} }
chapters.remove(emptyChapters); chapters.remove(emptyChapters);
// don't forget to tell the model that it's no longer being edited
this.model.set("editing", false);
} }
textbooks.trigger('render');
return this; return this;
} }
}); });
...@@ -178,7 +177,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({ ...@@ -178,7 +177,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({
var that = this; var that = this;
textbooks.each(function(textbook) { textbooks.each(function(textbook) {
var view; var view;
if (textbook === textbooks.editing) { if (textbook.get("editing")) {
view = new CMS.Views.EditTextbook({model: textbook}); view = new CMS.Views.EditTextbook({model: textbook});
} else { } else {
view = new CMS.Views.ShowTextbook({model: textbook}); view = new CMS.Views.ShowTextbook({model: textbook});
...@@ -193,11 +192,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({ ...@@ -193,11 +192,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({
}, },
addOne: function(e) { addOne: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
// if the existing edited textbook is empty, don't do anything this.collection.add([{editing: true}]);
if(this.collection.editing && this.collection.editing.isEmpty()) { return; }
var m = new this.collection.model();
this.collection.add(m);
this.collection.trigger("editOne", m);
} }
}); });
CMS.Views.EditChapter = Backbone.View.extend({ CMS.Views.EditChapter = Backbone.View.extend({
......
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