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