Uses events for ContentManager.save_notebook

Modifies ContentManager.save_notebook() to use events, so that the Notebook instance can listen for success or failure events.  Also moves some logic out of save_notebook()
This commit is contained in:
KesterTong 2014-07-23 18:19:06 -04:00 committed by Thomas Kluyver
parent 04fc61285f
commit dfcf14f26c
2 changed files with 37 additions and 20 deletions

View File

@ -125,15 +125,15 @@ define([
$.ajax(url, settings);
};
ContentManager.prototype.save_notebook = function(notebook, extra_settings) {
ContentManager.prototype.save_notebook = function(path, name, content,
extra_settings) {
var that = notebook;
// Create a JSON model to be sent to the server.
var model = {};
model.name = notebook.notebook_name;
model.path = notebook.notebook_path;
model.content = notebook.toJSON();
model.content.nbformat = notebook.nbformat;
model.content.nbformat_minor = notebook.nbformat_minor;
var model = {
name : name,
path : path,
content : content
};
// time the ajax call for autosave tuning purposes.
var start = new Date().getTime();
// We do the call with settings so we can set cache to false.
@ -143,20 +143,24 @@ define([
type : "PUT",
data : JSON.stringify(model),
contentType: 'application/json',
success : $.proxy(notebook.save_notebook_success, that, start),
error : $.proxy(notebook.save_notebook_error, that)
success : $.proxy(this.events.trigger, this.events,
'notebook_save_success.ContentManager',
$.extend(model, { start : start })),
error : function (xhr, status, error) {
that.events.trigger('notebook_save_error.ContentManager',
[xhr, status, error, model]);
}
};
if (extra_settings) {
for (var key in extra_settings) {
settings[key] = extra_settings[key];
}
}
notebook.events.trigger('notebook_saving.Notebook');
var url = utils.url_join_encode(
notebook.base_url,
this.base_url,
'api/notebooks',
notebook.notebook_path,
notebook.notebook_name
path,
name
);
$.ajax(url, settings);
};

View File

@ -191,6 +191,13 @@ define([
that.rename_error(data[0], data[1], data[2]);
});
this.content_manager.events.on('notebook_save_success.ContentManager',
$.proxy(this.save_notebook_success, this));
this.content_manager.events.on('notebook_save_error.ContentManager',
$.proxy(this.events.trigger, this.events,
'notebook_save_failed.Notebook'));
this.events.on('set_next_input.Notebook', function (event, data) {
var index = that.find_cell_index(data.cell);
var new_cell = that.insert_cell_below('code',index);
@ -1918,19 +1925,25 @@ define([
* @method save_notebook
*/
Notebook.prototype.save_notebook = function (extra_settings) {
this.content_manager.save_notebook(this, extra_settings);
var content = $.extend(this.toJSON(), {
nbformat : this.nbformat,
nbformat_minor : this.nbformat_minor
})
this.content_manager.save_notebook(this.notebook_path,
this.notebook_name,
content,
extra_settings);
};
/**
* Success callback for saving a notebook.
*
* @method save_notebook_success
* @param {Integer} start the time when the save request started
* @param {Object} data JSON representation of a notebook
* @param {String} status Description of response status
* @param {jqXHR} xhr jQuery Ajax object
* @param {Event} event The save notebook success event
* @param {Object} data dictionary of event data
* data.options start the time when the save request started
*/
Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
Notebook.prototype.save_notebook_success = function (event, data) {
this.set_dirty(false);
if (data.message) {
// save succeeded, but validation failed.
@ -1957,7 +1970,7 @@ define([
});
}
this.events.trigger('notebook_saved.Notebook');
this._update_autosave_interval(start);
this._update_autosave_interval(event.start);
if (this._checkpoint_after_save) {
this.create_checkpoint();
this._checkpoint_after_save = false;