mirror of https://github.com/TriliumNext/Notes
using server.method() instead of direct call to $.ajax - preparation for electron without network requests
parent
14001f67d8
commit
54c0ff15b3
@ -0,0 +1,51 @@
|
||||
const server = (function() {
|
||||
function initAjax() {
|
||||
$.ajaxSetup({
|
||||
headers: {
|
||||
'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function get(url) {
|
||||
return await ajax('GET', url);
|
||||
}
|
||||
|
||||
async function post(url, data) {
|
||||
return await ajax('POST', url, data);
|
||||
}
|
||||
|
||||
async function put(url, data) {
|
||||
return await ajax('PUT', url, data);
|
||||
}
|
||||
|
||||
async function remove(url) {
|
||||
return await ajax('DELETE', url);
|
||||
}
|
||||
|
||||
async function ajax(method, url, data) {
|
||||
const options = {
|
||||
url: baseApiUrl + url,
|
||||
type: method
|
||||
};
|
||||
|
||||
if (data) {
|
||||
options.data = JSON.stringify(data);
|
||||
options.contentType = "application/json";
|
||||
}
|
||||
|
||||
return await $.ajax(options).catch(e => {
|
||||
showError("Error when calling " + method + " " + url + ": " + e);
|
||||
});
|
||||
}
|
||||
|
||||
initAjax();
|
||||
|
||||
return {
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
remove,
|
||||
initAjax
|
||||
}
|
||||
})();
|
||||
@ -1,21 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
function syncNow() {
|
||||
$.ajax({
|
||||
url: baseApiUrl + 'sync/now',
|
||||
type: 'POST',
|
||||
success: result => {
|
||||
if (result.success) {
|
||||
showMessage("Sync finished successfully.");
|
||||
}
|
||||
else {
|
||||
if (result.message.length > 50) {
|
||||
result.message = result.message.substr(0, 50);
|
||||
}
|
||||
async function syncNow() {
|
||||
const result = await server.post('sync/now');
|
||||
|
||||
showError("Sync failed: " + result.message);
|
||||
}
|
||||
},
|
||||
error: () => showError("Sync failed for unknown reason.")
|
||||
});
|
||||
if (result.success) {
|
||||
showMessage("Sync finished successfully.");
|
||||
}
|
||||
else {
|
||||
if (result.message.length > 50) {
|
||||
result.message = result.message.substr(0, 50);
|
||||
}
|
||||
|
||||
showError("Sync failed: " + result.message);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue