Save newsletter before navigating away when clicked on "Next" button

This commit is contained in:
Tautvidas Sipavičius
2017-08-21 18:31:17 +03:00
parent 14810a22b5
commit dcb0b45c21
4 changed files with 40 additions and 34 deletions

View File

@ -65,15 +65,6 @@ define([
});
};
// For getting a promise after triggering save event
Module.saveAndProvidePromise = function(saveResult) {
var result = saveResult;
var promise = Module.save();
if (saveResult !== undefined) {
result.promise = promise;
}
};
Module.getThumbnail = function(element, options) {
var promise = html2canvas(element, options || {});
@ -169,7 +160,7 @@ define([
},
save: function() {
this.hideOptionContents();
App.getChannel().trigger('save');
App.getChannel().request('save');
},
beforeSave: function() {
// TODO: Add a loading animation instead
@ -289,7 +280,10 @@ define([
next: function() {
this.hideOptionContents();
if(!this.$('.mailpoet_save_next').hasClass('button-disabled')) {
window.location.href = App.getConfig().get('urls.send');
Module._cancelAutosave();
Module.save().done(function(response) {
window.location.href = App.getConfig().get('urls.send');
});
}
},
validateNewsletter: function(jsonObject) {
@ -326,15 +320,18 @@ define([
// may be requested
var AUTOSAVE_DELAY_DURATION = 1000;
// Cancel save timer if another change happens before it completes
if (saveTimeout) clearTimeout(saveTimeout);
Module._cancelAutosave();
saveTimeout = setTimeout(function() {
App.getChannel().trigger('save');
clearTimeout(saveTimeout);
saveTimeout = undefined;
App.getChannel().request('save').always(function() {
Module._cancelAutosave();
});
}, AUTOSAVE_DELAY_DURATION);
};
Module._cancelAutosave = function() {
if (saveTimeout) { clearTimeout(saveTimeout); saveTimeout = undefined; }
};
Module.beforeExitWithUnsavedChanges = function(e) {
if (saveTimeout) {
var message = MailPoet.I18n.t('unsavedChangesWillBeLost');
@ -349,13 +346,12 @@ define([
};
App.on('before:start', function(App, options) {
var Application = App;
Application.save = Module.saveAndProvidePromise;
Application.save = Module.save; //saveAndProvidePromise;
Application.getChannel().on('autoSave', Module.autoSave);
window.onbeforeunload = Module.beforeExitWithUnsavedChanges;
Application.getChannel().on('save', function(saveResult) { Application.save(saveResult); });
Application.getChannel().reply('save', App.save);
});
App.on('start', function(App, options) {