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