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