Skip to content

Commit 53cc639

Browse files
committed
move save up into DocumentDetailView
use async rather than deferred, which is easier to understand no need to track dirtyness on properties view, we can get that from the model directly
1 parent 5fc4742 commit 53cc639

File tree

5 files changed

+32
-118
lines changed

5 files changed

+32
-118
lines changed

indigo_app/static/javascript/indigo/models.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@
261261
}
262262
this.document.attributes.content = content;
263263
this.document.attributes.provision_eid = Indigo.Preloads.provisionEid;
264-
var result = this.document.save();
265-
// XXX works around https://github.com/Code4SA/indigo/issues/20 by not parsing
266-
// the response to the save() call
264+
const result = this.document.save();
265+
// don't re-parse the content in the response to the save() call
267266
delete this.document.attributes.content;
268267
this.document.setClean();
269268
this.trigger('sync');
@@ -511,12 +510,6 @@
511510
return url;
512511
},
513512

514-
setWork: function(work) {
515-
this.set('frbr_uri', work.get('frbr_uri'));
516-
this.work = work;
517-
this.trigger('change change:work');
518-
},
519-
520513
/** Get the Tradition description for this document's country.
521514
*/
522515
tradition: function() {

indigo_app/static/javascript/indigo/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ $(function() {
4545
}
4646
return doc;
4747
};
48+
49+
/**
50+
* This converts a jquery deferred into javascript promise/async function
51+
*/
52+
Indigo.deferredToAsync = async function(deferred) {
53+
await new Promise((resolve, reject) => {
54+
deferred.then(resolve).fail(reject);
55+
});
56+
};
4857
});

indigo_app/static/javascript/indigo/views/document.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
this.document.work = new Indigo.Work(Indigo.Preloads.work);
156156
this.document.issues = new Backbone.Collection();
157157

158+
this.document.on('sync', this.setClean, this);
158159
this.document.on('change', this.setDirty, this);
159160
this.document.on('change:draft', this.draftChanged, this);
160161

@@ -164,8 +165,6 @@
164165
this.cheatsheetView = new Indigo.CheatsheetView();
165166
this.titleView = new Indigo.DocumentTitleView({model: this.document});
166167
this.propertiesView = new Indigo.DocumentPropertiesView({model: this.document});
167-
this.propertiesView.on('dirty', this.setDirty, this);
168-
this.propertiesView.on('clean', this.setClean, this);
169168

170169
this.attachmentsView = new Indigo.DocumentAttachmentsView({document: this.document});
171170
this.attachmentsView.on('dirty', this.setDirty, this);
@@ -237,7 +236,7 @@
237236
},
238237

239238
isDirty: function(e) {
240-
return this.propertiesView.dirty || this.bodyEditorView.isDirty();
239+
return this.dirty || this.bodyEditorView.isDirty();
241240
},
242241

243242
setDirty: function() {
@@ -248,7 +247,7 @@
248247

249248
setClean: function() {
250249
// disable the save button if all views are clean
251-
if (!this.propertiesView.dirty && !this.bodyEditorView.dirty && !this.attachmentsView.dirty) {
250+
if (!this.bodyEditorView.dirty && !this.attachmentsView.dirty) {
252251
this.dirty = false;
253252
this.$saveBtn
254253
.prop('disabled', true)
@@ -283,44 +282,31 @@
283282
}
284283
},
285284

286-
save: function() {
287-
var self = this;
288-
var deferred = null;
289-
285+
save: async function() {
290286
if (!this.bodyEditorView.canCancelEdits()) return;
291287

292-
// always save properties if we save content
293-
this.propertiesView.dirty = this.propertiesView.dirty || this.bodyEditorView.dirty;
294-
295-
var fail = function() {
296-
self.$saveBtn
297-
.prop('disabled', false)
298-
.find('.fa')
299-
.removeClass('fa-pulse fa-spinner')
300-
.addClass('fa-save');
301-
self.$menu.find('.save').removeClass('disabled');
302-
};
303-
304288
this.$saveBtn
305289
.prop('disabled', true)
306290
.find('.fa')
307291
.removeClass('fa-save')
308292
.addClass('fa-pulse fa-spinner');
309-
this.$menu.find('.save').addClass('disabled');
310-
311-
deferred = $.Deferred().resolve();
312-
313-
// We save the content first, and then save
314-
// the properties on top of it, so that content
315-
// properties that change metadata in the content
316-
// take precendence.
317-
deferred.then(function() {
318-
self.bodyEditorView.save().then(function() {
319-
self.propertiesView.save().then(function() {
320-
self.attachmentsView.save().fail(fail);
321-
}).fail(fail);
322-
}).fail(fail);
323-
}).fail(fail);
293+
294+
try {
295+
// this saves the content and the document properties together
296+
await Indigo.deferredToAsync(this.documentContent.save());
297+
await Indigo.deferredToAsync(this.attachmentsView.save());
298+
299+
// TODO: a better way of reloading the page (will redirect to provision chooser for now)
300+
if (this.bodyEditorView.sourceEditor.aknTextEditor.reloadOnSave) {
301+
window.location.reload();
302+
}
303+
} catch {
304+
this.$saveBtn
305+
.prop('disabled', false)
306+
.find('.fa')
307+
.removeClass('fa-pulse fa-spinner')
308+
.addClass('fa-save');
309+
}
324310
},
325311

326312
delete: function() {

indigo_app/static/javascript/indigo/views/document_editor.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -553,42 +553,5 @@
553553
canCancelEdits: function() {
554554
return this.sourceEditor.confirmAndDiscardChanges();
555555
},
556-
557-
// Save the content of the editor, returns a Deferred
558-
save: function() {
559-
var self = this,
560-
deferred = $.Deferred();
561-
562-
function ok() { deferred.resolve(); }
563-
function fail() { deferred.reject(); }
564-
565-
if (!this.dirty) {
566-
// don't do anything if it hasn't changed
567-
ok();
568-
569-
} else {
570-
// ask the editor to returns its contents
571-
this.sourceEditor
572-
.saveChanges()
573-
.done(function() {
574-
// save the model
575-
self.saveModel().done(ok).fail(fail);
576-
})
577-
.fail(fail);
578-
}
579-
580-
// TODO: a better way of reloading the page (will redirect to provision chooser for now)
581-
if (this.sourceEditor.aknTextEditor.reloadOnSave) {
582-
window.location.reload();
583-
}
584-
585-
return deferred;
586-
},
587-
588-
// Save the content of the document, returns a Deferred
589-
saveModel: function() {
590-
return this.documentContent.save();
591-
},
592-
593556
});
594557
})(window);

indigo_app/static/javascript/indigo/views/document_properties.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
return (!val || val.trim() === "") ? null : val;
1010
}
1111

12-
function bool(val) {
13-
return val == "1";
14-
}
15-
1612
// Handle the document properties form, and saving them back to the server.
1713
Indigo.DocumentPropertiesView = Backbone.View.extend({
1814
el: '.document-properties-view',
@@ -30,13 +26,8 @@
3026
work: this.model,
3127
});
3228

33-
this.dirty = false;
34-
this.listenTo(this.model, 'change', this.setDirty);
35-
this.listenTo(this.model, 'sync', this.setClean);
3629
this.listenTo(this.model, 'change:draft change:frbr_uri change:language change:expression_date sync', this.showPublishedUrl);
37-
3830
this.stickit();
39-
4031
this.render();
4132
},
4233

@@ -47,34 +38,6 @@
4738
this.$el.find('#document_published_url').attr('href', url).text(url);
4839
},
4940

50-
setDirty: function() {
51-
if (!this.dirty) {
52-
this.dirty = true;
53-
this.trigger('dirty');
54-
}
55-
},
56-
57-
setClean: function() {
58-
if (this.dirty) {
59-
this.dirty = false;
60-
this.trigger('clean');
61-
}
62-
},
63-
64-
save: function() {
65-
var self = this;
66-
// TODO: validation
67-
68-
// don't do anything if it hasn't changed
69-
if (!this.dirty) {
70-
return $.Deferred().resolve();
71-
}
72-
73-
return this.model.save().done(function() {
74-
self.setClean();
75-
});
76-
},
77-
7841
render: function() {
7942
var work = this.model.work;
8043

0 commit comments

Comments
 (0)