Commit a608d8a3 by David Baumgold

PDF Textbooks: handle model deletion

Previously, when a model was deleted, the entire collection would save itself
to the server again. Now, we just call model.destroy(), which doesn't affect
other models.
parent 8f2077b5
...@@ -11,20 +11,22 @@ beforeEach -> ...@@ -11,20 +11,22 @@ beforeEach ->
return trimmedText.indexOf(text) != -1; return trimmedText.indexOf(text) != -1;
describe "CMS.Views.ShowTextbook", -> describe "CMS.Views.ShowTextbook", ->
describe "Basic", -> tpl = readFixtures('show-textbook.underscore')
tpl = readFixtures('show-textbook.underscore')
beforeEach -> beforeEach ->
setFixtures($("<script>", {id: "show-textbook-tpl", type: "text/template"}).text(tpl)) setFixtures($("<script>", {id: "show-textbook-tpl", type: "text/template"}).text(tpl))
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", id: "0life-sciences"})
@collection = new CMS.Collections.TextbookSet() spyOn(@model, "destroy").andCallThrough()
spyOn(@collection, 'save') @collection = new CMS.Collections.TextbookSet([@model])
@collection.add(@model) @view = new CMS.Views.ShowTextbook({model: @model})
@view = new CMS.Views.ShowTextbook({model: @model})
@promptSpies = spyOnConstructor(CMS.Views.Prompt, "Warning", ["show", "hide"])
@promptSpies.show.andReturn(@promptSpies)
describe "Basic", ->
it "should render properly", -> it "should render properly", ->
@view.render() @view.render()
expect(@view.$el).toContainText("Life Sciences") expect(@view.$el).toContainText("Life Sciences")
...@@ -34,21 +36,13 @@ describe "CMS.Views.ShowTextbook", -> ...@@ -34,21 +36,13 @@ describe "CMS.Views.ShowTextbook", ->
expect(@model.get("editing")).toBeTruthy() expect(@model.get("editing")).toBeTruthy()
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"])
@view.render().$(".delete").click() @view.render().$(".delete").click()
expect(promptSpies.constructor).toHaveBeenCalled() expect(@promptSpies.constructor).toHaveBeenCalled()
ctorOptions = promptSpies.constructor.mostRecentCall.args[0] ctorOptions = @promptSpies.constructor.mostRecentCall.args[0]
expect(ctorOptions.title).toMatch(/Life Sciences/) expect(ctorOptions.title).toMatch(/Life Sciences/)
# hasn't actually been removed # hasn't actually been removed
expect(@model.destroy).not.toHaveBeenCalled()
expect(@collection).toContain(@model) expect(@collection).toContain(@model)
expect(@collection.save).not.toHaveBeenCalled()
# run the primary function to indicate confirmation
view = jasmine.createSpyObj('view', ['hide'])
context = jasmine.createSpy('context')
ctorOptions.actions.primary.click.call(context, view)
# now it's been removed
expect(@collection).not.toContain(@model)
expect(@collection.save).toHaveBeenCalled()
it "should show chapters appropriately", -> it "should show chapters appropriately", ->
@model.get("chapters").add([{}, {}, {}]) @model.get("chapters").add([{}, {}, {}])
...@@ -62,7 +56,36 @@ describe "CMS.Views.ShowTextbook", -> ...@@ -62,7 +56,36 @@ describe "CMS.Views.ShowTextbook", ->
@view.render().$(".hide-chapters").click() @view.render().$(".hide-chapters").click()
expect(@model.get('showChapters')).toBeFalsy() expect(@model.get('showChapters')).toBeFalsy()
describe "AJAX", ->
beforeEach ->
@requests = requests = []
@xhr = sinon.useFakeXMLHttpRequest()
@xhr.onCreate = (xhr) -> requests.push(xhr)
@savingSpies = spyOnConstructor(CMS.Views.Notification, "Saving",
["show", "hide"])
@savingSpies.show.andReturn(@savingSpies)
afterEach ->
@xhr.restore()
it "should destroy itself on confirmation", ->
@view.render().$(".delete").click()
ctorOptions = @promptSpies.constructor.mostRecentCall.args[0]
# run the primary function to indicate confirmation
ctorOptions.actions.primary.click(@promptSpies)
# AJAX request has been sent, but not yet returned
expect(@model.destroy).toHaveBeenCalled()
expect(@requests.length).toEqual(1)
expect(@savingSpies.constructor).toHaveBeenCalled()
expect(@savingSpies.show).toHaveBeenCalled()
expect(@savingSpies.hide).not.toHaveBeenCalled()
savingOptions = @savingSpies.constructor.mostRecentCall.args[0]
expect(savingOptions.title).toMatch(/Deleting/)
# return a success response
@requests[0].respond(200)
expect(@savingSpies.hide).toHaveBeenCalled()
expect(@collection.contains(@model)).toBeFalsy()
describe "CMS.Views.EditTextbook", -> describe "CMS.Views.EditTextbook", ->
describe "Basic", -> describe "Basic", ->
......
...@@ -31,25 +31,24 @@ CMS.Views.ShowTextbook = Backbone.View.extend({ ...@@ -31,25 +31,24 @@ CMS.Views.ShowTextbook = Backbone.View.extend({
text: gettext("Delete"), text: gettext("Delete"),
click: function(view) { click: function(view) {
view.hide(); view.hide();
collection.remove(textbook);
var delmsg = new CMS.Views.Notification.Saving({ var delmsg = new CMS.Views.Notification.Saving({
title: gettext("Deleting&hellip;"), title: gettext("Deleting&hellip;"),
closeIcon: false, closeIcon: false,
minShown: 1250 minShown: 1250
}).show(); }).show();
collection.save({ textbook.destroy({
complete: function() { complete: function() {
delmsg.hide(); delmsg.hide();
} }
}); });
} }
}, },
secondary: [{ secondary: {
text: gettext("Cancel"), text: gettext("Cancel"),
click: function(view) { click: function(view) {
view.hide(); view.hide();
} }
}] }
} }
}).show(); }).show();
}, },
...@@ -167,6 +166,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({ ...@@ -167,6 +166,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({
initialize: function() { initialize: function() {
this.emptyTemplate = _.template($("#no-textbooks-tpl").text()); this.emptyTemplate = _.template($("#no-textbooks-tpl").text());
this.listenTo(this.collection, 'all', this.render); this.listenTo(this.collection, 'all', this.render);
this.listenTo(this.collection, 'destroy', this.handleDestroy);
}, },
tagName: "div", tagName: "div",
className: "textbooks-list", className: "textbooks-list",
...@@ -195,6 +195,9 @@ CMS.Views.ListTextbooks = Backbone.View.extend({ ...@@ -195,6 +195,9 @@ CMS.Views.ListTextbooks = Backbone.View.extend({
addOne: function(e) { addOne: function(e) {
if(e && e.preventDefault) { e.preventDefault(); } if(e && e.preventDefault) { e.preventDefault(); }
this.collection.add([{editing: true}]); this.collection.add([{editing: true}]);
},
handleDestroy: function(model, collection, options) {
collection.remove(model);
} }
}); });
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