##// END OF EJS Templates
All aboard the promise train
Thomas Kluyver -
Show More
@@ -92,14 +92,13 b' define(['
92 92 // Create a new notebook in the same path as the current
93 93 // notebook's path.
94 94 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
95 that.contents.new_untitled(parent, {
96 type: "notebook",
97 success: function (data) {
95 that.contents.new_untitled(parent, {type: "notebook"}).then(
96 function (data) {
98 97 w.location = utils.url_join_encode(
99 98 that.base_url, 'notebooks', data.path
100 99 );
101 100 },
102 error: function(error) {
101 function(error) {
103 102 w.close();
104 103 dialog.modal({
105 104 title : 'Creating Notebook Failed',
@@ -107,7 +106,7 b' define(['
107 106 buttons : {'OK' : {'class' : 'btn-primary'}}
108 107 });
109 108 }
110 });
109 );
111 110 });
112 111 this.element.find('#open_notebook').click(function () {
113 112 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
@@ -1902,12 +1902,12 b' define(['
1902 1902 var start = new Date().getTime();
1903 1903
1904 1904 var that = this;
1905 this.contents.save(this.notebook_path, model, {
1906 success: $.proxy(this.save_notebook_success, this, start),
1907 error: function (error) {
1905 this.contents.save(this.notebook_path, model).then(
1906 $.proxy(this.save_notebook_success, this, start),
1907 function (error) {
1908 1908 that.events.trigger('notebook_save_failed.Notebook', error);
1909 1909 }
1910 });
1910 );
1911 1911 };
1912 1912
1913 1913 /**
@@ -2025,17 +2025,17 b' define(['
2025 2025 var base_url = this.base_url;
2026 2026 var w = window.open();
2027 2027 var parent = utils.url_path_split(this.notebook_path)[0];
2028 this.contents.copy(this.notebook_path, parent, {
2029 success: function (data) {
2028 this.contents.copy(this.notebook_path, parent).then(
2029 function (data) {
2030 2030 w.location = utils.url_join_encode(
2031 2031 base_url, 'notebooks', data.path
2032 2032 );
2033 2033 },
2034 error : function(error) {
2034 function(error) {
2035 2035 w.close();
2036 2036 console.log(error);
2037 },
2038 });
2037 }
2038 );
2039 2039 };
2040 2040
2041 2041 Notebook.prototype.rename = function (new_name) {
@@ -2046,15 +2046,15 b' define(['
2046 2046 var that = this;
2047 2047 var parent = utils.url_path_split(this.notebook_path)[0];
2048 2048 var new_path = utils.url_path_join(parent, new_name);
2049 this.contents.rename(this.notebook_path, new_path, {
2050 success: function (json) {
2049 this.contents.rename(this.notebook_path, new_path).then(
2050 function (json) {
2051 2051 that.notebook_name = json.name;
2052 2052 that.notebook_path = json.path;
2053 2053 that.session.rename_notebook(json.path);
2054 2054 that.events.trigger('notebook_renamed.Notebook', json);
2055 2055 },
2056 error: $.proxy(this.rename_error, this)
2057 });
2056 $.proxy(this.rename_error, this)
2057 );
2058 2058 };
2059 2059
2060 2060 Notebook.prototype.delete = function () {
@@ -2326,12 +2326,12 b' define(['
2326 2326 */
2327 2327 Notebook.prototype.list_checkpoints = function () {
2328 2328 var that = this;
2329 this.contents.list_checkpoints(this.notebook_path, {
2330 success: $.proxy(this.list_checkpoints_success, this),
2331 error: function(error) {
2329 this.contents.list_checkpoints(this.notebook_path).then(
2330 $.proxy(this.list_checkpoints_success, this),
2331 function(error) {
2332 2332 that.events.trigger('list_checkpoints_failed.Notebook', error);
2333 2333 }
2334 });
2334 );
2335 2335 };
2336 2336
2337 2337 /**
@@ -2358,12 +2358,12 b' define(['
2358 2358 */
2359 2359 Notebook.prototype.create_checkpoint = function () {
2360 2360 var that = this;
2361 this.contents.create_checkpoint(this.notebook_path, {
2362 success: $.proxy(this.create_checkpoint_success, this),
2363 error: function (error) {
2361 this.contents.create_checkpoint(this.notebook_path).then(
2362 $.proxy(this.create_checkpoint_success, this),
2363 function (error) {
2364 2364 that.events.trigger('checkpoint_failed.Notebook', error);
2365 2365 }
2366 });
2366 );
2367 2367 };
2368 2368
2369 2369 /**
@@ -2428,13 +2428,12 b' define(['
2428 2428 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2429 2429 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2430 2430 var that = this;
2431 this.contents.restore_checkpoint(this.notebook_path,
2432 checkpoint, {
2433 success: $.proxy(this.restore_checkpoint_success, this),
2434 error: function (error) {
2431 this.contents.restore_checkpoint(this.notebook_path, checkpoint).then(
2432 $.proxy(this.restore_checkpoint_success, this),
2433 function (error) {
2435 2434 that.events.trigger('checkpoint_restore_failed.Notebook', error);
2436 2435 }
2437 });
2436 );
2438 2437 };
2439 2438
2440 2439 /**
@@ -2456,13 +2455,12 b' define(['
2456 2455 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2457 2456 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2458 2457 var that = this;
2459 this.contents.delete_checkpoint(this.notebook_path,
2460 checkpoint, {
2461 success: $.proxy(this.delete_checkpoint_success, this),
2462 error: function (error) {
2458 this.contents.delete_checkpoint(this.notebook_path, checkpoint).then(
2459 $.proxy(this.delete_checkpoint_success, this),
2460 function (error) {
2463 2461 that.events.trigger('checkpoint_delete_failed.Notebook', error);
2464 2462 }
2465 });
2463 );
2466 2464 };
2467 2465
2468 2466 /**
@@ -112,33 +112,31 b' define(['
112 112 type : "POST",
113 113 data: data,
114 114 dataType : "json",
115 success : options.success || function() {},
116 error : this.create_basic_error_handler(options.error)
117 115 };
118 $.ajax(this.api_url(path), settings);
116 return utils.promising_ajax(this.api_url(path), settings);
119 117 };
120 118
121 Contents.prototype.delete = function(path, options) {
122 var error_callback = options.error || function() {};
119 Contents.prototype.delete = function(path) {
123 120 var settings = {
124 121 processData : false,
125 122 type : "DELETE",
126 123 dataType : "json",
127 success : options.success || function() {},
128 error : function(xhr, status, error) {
124 };
125 var url = this.api_url(path);
126 return utils.promising_ajax(url, settings).catch(
127 // Translate certain errors to more specific ones.
128 function(error) {
129 129 // TODO: update IPEP27 to specify errors more precisely, so
130 130 // that error types can be detected here with certainty.
131 if (xhr.status === 400) {
132 error_callback(new Contents.DirectoryNotEmptyError());
131 if (error.xhr.status === 400) {
132 return Promise.reject(new Contents.DirectoryNotEmptyError());
133 133 }
134 error_callback(utils.wrap_ajax_error(xhr, status, error));
134 return Promise.reject(error);
135 135 }
136 };
137 var url = this.api_url(path);
138 $.ajax(url, settings);
136 );
139 137 };
140 138
141 Contents.prototype.rename = function(path, new_path, options) {
139 Contents.prototype.rename = function(path, new_path) {
142 140 var data = {path: new_path};
143 141 var settings = {
144 142 processData : false,
@@ -146,28 +144,24 b' define(['
146 144 data : JSON.stringify(data),
147 145 dataType: "json",
148 146 contentType: 'application/json',
149 success : options.success || function() {},
150 error : this.create_basic_error_handler(options.error)
151 147 };
152 148 var url = this.api_url(path);
153 $.ajax(url, settings);
149 return utils.promising_ajax(url, settings);
154 150 };
155 151
156 Contents.prototype.save = function(path, model, options) {
152 Contents.prototype.save = function(path, model) {
157 153 // We do the call with settings so we can set cache to false.
158 154 var settings = {
159 155 processData : false,
160 156 type : "PUT",
161 157 data : JSON.stringify(model),
162 158 contentType: 'application/json',
163 success : options.success || function() {},
164 error : this.create_basic_error_handler(options.error)
165 159 };
166 160 var url = this.api_url(path);
167 $.ajax(url, settings);
161 return utils.promising_ajax(url, settings);
168 162 };
169 163
170 Contents.prototype.copy = function(from_file, to_dir, options) {
164 Contents.prototype.copy = function(from_file, to_dir) {
171 165 // Copy a file into a given directory via POST
172 166 // The server will select the name of the copied file
173 167 var url = this.api_url(to_dir);
@@ -177,54 +171,44 b' define(['
177 171 type: "POST",
178 172 data: JSON.stringify({copy_from: from_file}),
179 173 dataType : "json",
180 success: options.success || function() {},
181 error: this.create_basic_error_handler(options.error)
182 174 };
183 $.ajax(url, settings);
175 return utils.promising_ajax(url, settings);
184 176 };
185 177
186 178 /**
187 179 * Checkpointing Functions
188 180 */
189 181
190 Contents.prototype.create_checkpoint = function(path, options) {
182 Contents.prototype.create_checkpoint = function(path) {
191 183 var url = this.api_url(path, 'checkpoints');
192 184 var settings = {
193 185 type : "POST",
194 success: options.success || function() {},
195 error : this.create_basic_error_handler(options.error)
196 186 };
197 $.ajax(url, settings);
187 return utils.promising_ajax(url, settings);
198 188 };
199 189
200 Contents.prototype.list_checkpoints = function(path, options) {
190 Contents.prototype.list_checkpoints = function(path) {
201 191 var url = this.api_url(path, 'checkpoints');
202 192 var settings = {
203 193 type : "GET",
204 success: options.success,
205 error : this.create_basic_error_handler(options.error)
206 194 };
207 $.ajax(url, settings);
195 return utils.promising_ajax(url, settings);
208 196 };
209 197
210 Contents.prototype.restore_checkpoint = function(path, checkpoint_id, options) {
198 Contents.prototype.restore_checkpoint = function(path, checkpoint_id) {
211 199 var url = this.api_url(path, 'checkpoints', checkpoint_id);
212 200 var settings = {
213 201 type : "POST",
214 success: options.success || function() {},
215 error : this.create_basic_error_handler(options.error)
216 202 };
217 $.ajax(url, settings);
203 return utils.promising_ajax(url, settings);
218 204 };
219 205
220 Contents.prototype.delete_checkpoint = function(path, checkpoint_id, options) {
206 Contents.prototype.delete_checkpoint = function(path, checkpoint_id) {
221 207 var url = this.api_url(path, 'checkpoints', checkpoint_id);
222 208 var settings = {
223 209 type : "DELETE",
224 success: options.success || function() {},
225 error : this.create_basic_error_handler(options.error)
226 210 };
227 $.ajax(url, settings);
211 return utils.promising_ajax(url, settings);
228 212 };
229 213
230 214 /**
@@ -65,14 +65,13 b' require(['
65 65
66 66 $('#new_notebook').click(function (e) {
67 67 var w = window.open();
68 contents.new_untitled(common_options.notebook_path, {
69 type: "notebook",
70 success: function (data) {
68 contents.new_untitled(common_options.notebook_path, {type: "notebook"}).then(
69 function (data) {
71 70 w.location = utils.url_join_encode(
72 71 common_options.base_url, 'notebooks', data.path
73 72 );
74 73 },
75 error: function(error) {
74 function(error) {
76 75 w.close();
77 76 dialog.modal({
78 77 title : 'Creating Notebook Failed',
@@ -80,7 +79,7 b' require(['
80 79 buttons : {'OK' : {'class' : 'btn-primary'}}
81 80 });
82 81 }
83 });
82 );
84 83 });
85 84
86 85 var interval_id=0;
@@ -328,11 +328,11 b' define(['
328 328 Delete : {
329 329 class: "btn-danger",
330 330 click: function() {
331 notebooklist.contents.delete(path, {
332 success: function() {
331 notebooklist.contents.delete(path).then(
332 function() {
333 333 notebooklist.notebook_deleted(path);
334 334 }
335 });
335 );
336 336 }
337 337 },
338 338 Cancel : {}
@@ -414,13 +414,11 b' define(['
414 414 }
415 415 filedata = item.data('filedata');
416 416
417 var settings = {
418 success : function () {
417 var on_success = function () {
419 418 item.removeClass('new-file');
420 419 that.add_link(model, item);
421 420 that.add_delete_button(item);
422 421 that.session_list.load_sessions();
423 },
424 422 };
425 423
426 424 var exists = false;
@@ -436,7 +434,7 b' define(['
436 434 Overwrite : {
437 435 class: "btn-danger",
438 436 click: function () {
439 that.contents.save(path, model, settings);
437 that.contents.save(path, model).then(on_success);
440 438 }
441 439 },
442 440 Cancel : {
@@ -445,7 +443,7 b' define(['
445 443 }
446 444 });
447 445 } else {
448 that.contents.save(path, model, settings);
446 that.contents.save(path, model).then(on_success);
449 447 }
450 448
451 449 return false;
General Comments 0
You need to be logged in to leave comments. Login now