RestPersisnteceManager reimplemented with ajax

This commit is contained in:
Ezequiel Bergamaschi 2014-08-03 16:50:06 -03:00
parent d41a261897
commit 8cb45c587f
2 changed files with 23 additions and 49 deletions

View File

@ -16,7 +16,6 @@
* limitations under the License. * limitations under the License.
*/ */
//FIXME: remove Request class
mindplot.RESTPersistenceManager = new Class({ mindplot.RESTPersistenceManager = new Class({
Extends:mindplot.PersistenceManager, Extends:mindplot.PersistenceManager,
initialize:function (options) { initialize:function (options) {
@ -56,38 +55,38 @@ mindplot.RESTPersistenceManager = new Class({
persistence.onSave = false; persistence.onSave = false;
}, 10000); }, 10000);
var request = new Request({ $.ajax({
url: this.documentUrl.replace("{id}", mapId) + "?" + query, url: this.documentUrl.replace("{id}", mapId) + "?" + query,
headers:{"Content-Type":"application/json; charset=utf-8", "Accept":"application/json"},
method:'put', method:'put',
async:!sync, async:!sync,
dataType: "json",
data: data,
onSuccess:function (responseText, responseXML) { success: function (data, textStatus, jqXHRresponseText) {
persistence.timestamp = responseText; persistence.timestamp = jqXHRresponseText;
events.onSuccess(); events.onSuccess();
}, },
error: function (jqXHR, textStatus, errorThrown) {
onException:function (headerName, value) {
events.onError(persistence._buildError()); events.onError(persistence._buildError());
}, },
complete: function () {
onComplete:function () {
// Clear event timeout ... // Clear event timeout ...
if (persistence.clearTimeout) { if (persistence.clearTimeout) {
clearTimeout(persistence.clearTimeout); clearTimeout(persistence.clearTimeout);
} }
persistence.onSave = false; persistence.onSave = false;
}, },
fail:function (xhr, textStatus) {
onFailure:function (xhr) {
var responseText = xhr.responseText; var responseText = xhr.responseText;
var userMsg = {severity:"SEVERE", message:$msg('SAVE_COULD_NOT_BE_COMPLETED')}; var userMsg = {severity:"SEVERE", message:$msg('SAVE_COULD_NOT_BE_COMPLETED')};
var contentType = this.getHeader("Content-Type"); var contentType = xhr.getResponseHeader("Content-Type");
if (contentType != null && contentType.indexOf("application/json") != -1) { if (contentType != null && contentType.indexOf("application/json") != -1) {
var serverMsg = null; var serverMsg = null;
try { try {
serverMsg = JSON.decode(responseText); serverMsg = $.parseJSON(responseText);
serverMsg = serverMsg.globalSeverity ? serverMsg : null; serverMsg = serverMsg.globalSeverity ? serverMsg : null;
} catch (e) { } catch (e) {
// Message could not be decoded ... // Message could not be decoded ...
@ -101,52 +100,29 @@ mindplot.RESTPersistenceManager = new Class({
} }
events.onError(userMsg); events.onError(userMsg);
persistence.onSave = false; persistence.onSave = false;
}, }
headers:{"Content-Type":"application/json; charset=utf-8", "Accept":"application/json"},
emulation:false,
urlEncoded:false
}); });
request.put(JSON.encode(data));
} }
}, },
discardChanges:function (mapId) { discardChanges:function (mapId) {
var request = new Request({ $.ajax({
url:this.revertUrl.replace("{id}", mapId), url:this.revertUrl.replace("{id}", mapId),
async:false, async:false,
method:'post', method:'post',
onSuccess:function () { headers:{"Content-Type":"application/json; charset=utf-8", "Accept":"application/json"}
},
onException:function () {
},
onFailure:function () {
},
headers:{"Content-Type":"application/json; charset=utf-8", "Accept":"application/json"},
emulation:false,
urlEncoded:false
}); });
request.post();
}, },
unlockMap:function (mindmap) { unlockMap:function (mindmap) {
var mapId = mindmap.getId(); var mapId = mindmap.getId();
var request = new Request({ $.ajax({
url:this.lockUrl.replace("{id}", mapId), url:this.lockUrl.replace("{id}", mapId),
async:false, async:false,
method:'put', method:'put',
onSuccess:function () {
},
onException:function () {
},
onFailure:function () {
},
headers:{"Content-Type":"text/plain"}, headers:{"Content-Type":"text/plain"},
emulation:false, data: "false"
urlEncoded:false
}); });
request.put("false");
}, },
_buildError:function (jsonSeverResponse) { _buildError:function (jsonSeverResponse) {

View File

@ -14,11 +14,11 @@ function createStorageManager(mindplot) {
saveMapXml : function(mapId, mapXml, pref, saveHistory, events) { saveMapXml : function(mapId, mapXml, pref, saveHistory, events) {
var url = this.backendUrl + mapId; var url = this.backendUrl + mapId;
var xmlRequest = new Request({ $.ajax({
url: url, url: url,
method: 'post', method: 'post',
async: false, async: false,
onSuccess: function(responseText) { success: function(responseText) {
events.onSuccess(); events.onSuccess();
}, },
onError: function (text, error) { onError: function (text, error) {
@ -26,20 +26,18 @@ function createStorageManager(mindplot) {
events.onError(); events.onError();
} }
}); });
xmlRequest.send();
}, },
loadMapDom : function(mapId) { loadMapDom : function(mapId) {
var xml; var xml;
var xmlRequest = new Request({ $.ajax({
url: this.backendUrl + mapId, url: this.backendUrl + mapId,
method: 'get', method: 'get',
async: false, async: false,
onSuccess: function(responseText) { success: function(responseText) {
xml = responseText; xml = responseText;
} }
}); });
xmlRequest.send();
// If I could not load it from a file, hard code one. // If I could not load it from a file, hard code one.
if (xml == null) { if (xml == null) {