Show More
@@ -1,80 +1,63 | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'jquery', |
|
5 | 'jquery', | |
6 | 'base/js/utils', |
|
6 | 'base/js/utils', | |
7 | ], |
|
7 | ], | |
8 | function($, utils) { |
|
8 | function($, utils) { | |
9 | var ConfigSection = function(section_name, options) { |
|
9 | var ConfigSection = function(section_name, options) { | |
10 | this.section_name = section_name; |
|
10 | this.section_name = section_name; | |
11 | this.base_url = options.base_url; |
|
11 | this.base_url = options.base_url; | |
12 | this.data = {}; |
|
12 | this.data = {}; | |
13 |
|
13 | |||
14 | var that = this; |
|
14 | var that = this; | |
15 |
|
15 | |||
16 | /* .loaded is a promise, fulfilled the first time the config is loaded |
|
16 | /* .loaded is a promise, fulfilled the first time the config is loaded | |
17 | * from the server. Code can do: |
|
17 | * from the server. Code can do: | |
18 | * conf.loaded.then(function() { ... using conf.data ... }); |
|
18 | * conf.loaded.then(function() { ... using conf.data ... }); | |
19 | */ |
|
19 | */ | |
20 | this._one_load_finished = false; |
|
20 | this._one_load_finished = false; | |
21 | this.loaded = new Promise(function(resolve, reject) { |
|
21 | this.loaded = new Promise(function(resolve, reject) { | |
22 | that._finish_firstload = resolve; |
|
22 | that._finish_firstload = resolve; | |
23 | }); |
|
23 | }); | |
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 | ConfigSection.prototype.api_url = function() { |
|
26 | ConfigSection.prototype.api_url = function() { | |
27 | return utils.url_join_encode(this.base_url, 'api/config', that.section_name); |
|
27 | return utils.url_join_encode(this.base_url, 'api/config', that.section_name); | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | ConfigSection.prototype._load_done = function() { |
|
30 | ConfigSection.prototype._load_done = function() { | |
31 | if (!this._one_load_finished) { |
|
31 | if (!this._one_load_finished) { | |
32 | this._one_load_finished = true; |
|
32 | this._one_load_finished = true; | |
33 | this._finish_firstload(); |
|
33 | this._finish_firstload(); | |
34 | } |
|
34 | } | |
35 | }; |
|
35 | }; | |
36 |
|
36 | |||
37 | ConfigSection.prototype.load = function() { |
|
37 | ConfigSection.prototype.load = function() { | |
38 | var p = new Promise(function(resolve, reject) { |
|
38 | return utils.promising_ajax(this.api_url(), { | |
39 | $.ajax(this.api_url(), { |
|
39 | cache: false, | |
40 | cache : false, |
|
40 | type: "GET", | |
41 |
|
|
41 | dataType: "json", | |
42 | dataType : "json", |
|
42 | }).then(function(data) { | |
43 | success: function(data, status, jqXHR) { |
|
43 | this.data = data; | |
44 | this.data = data; |
|
44 | this._load_done(); | |
45 | this._load_done(); |
|
45 | return data; | |
46 | resolve(data); |
|
|||
47 | }, |
|
|||
48 | error: function(jqXHR, status, error) { |
|
|||
49 | // Should never happen; mark as loaded so things don't keep |
|
|||
50 | // waiting. |
|
|||
51 | this._load_done(); |
|
|||
52 | utils.log_ajax_error(jqXHR, status, error); |
|
|||
53 | reject(utils.wrap_ajax_error(jqXHR, status, error)); |
|
|||
54 | } |
|
|||
55 | }); |
|
|||
56 | }); |
|
46 | }); | |
57 | }; |
|
47 | }; | |
58 |
|
48 | |||
59 | ConfigSection.prototype.update = function(newdata) { |
|
49 | ConfigSection.prototype.update = function(newdata) { | |
60 | return new Promise(function(resolve, reject) { |
|
50 | return utils.promising_ajax(this.api_url(), { | |
61 | $.ajax(this.api_url(), { |
|
51 | processData: false, | |
62 | processData: false, |
|
52 | type : "PATCH", | |
63 | type : "PATCH", |
|
53 | data: JSON.stringify(newdata), | |
64 | data: JSON.stringify(newdata), |
|
54 | dataType : "json", | |
65 |
|
|
55 | contentType: 'application/json', | |
66 | contentType: 'application/json', |
|
56 | }).then(function(data) { | |
67 | success: function(data, status, jqXHR) { |
|
57 | this.data = data; | |
68 | this.data = data; |
|
58 | this._load_done(); | |
69 | this._load_done(); |
|
59 | return data; | |
70 | resolve(data); |
|
|||
71 | }, |
|
|||
72 | error: function(jqXHR, status, error) { |
|
|||
73 | utils.log_ajax_error(jqXHR, status, error); |
|
|||
74 | reject(utils.wrap_ajax_error(jqXHR, status, error)); |
|
|||
75 | } |
|
|||
76 | }); |
|
|||
77 | }); |
|
60 | }); | |
78 | }; |
|
61 | }; | |
79 |
|
62 | |||
80 | }); |
|
63 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now