1
0
Fork 0

sync calls to async

This commit is contained in:
hkernbach 2016-02-20 01:20:29 +01:00
parent 3743cce37e
commit 553875b691
8 changed files with 276 additions and 244 deletions

View File

@ -1,5 +1,5 @@
/*jshint unused: false */
/*global window, $, document, _ */
/*global window, $, document, arangoHelper, _ */
(function() {
"use strict";
@ -443,16 +443,28 @@
this.arangoDocumentStore = a;
},
collectionApiType: function (identifier, refresh) {
collectionApiType: function (identifier, refresh, toRun) {
// set "refresh" to disable caching collection type
if (refresh || this.CollectionTypes[identifier] === undefined) {
this.CollectionTypes[identifier] = this.arangoDocumentStore
.getCollectionInfo(identifier).type;
var callback = function(error, data, toRun) {
if (error) {
arangoHelper.arangoError("Error", "Could not detect collection type");
}
else {
this.CollectionTypes[identifier] = data.type;
if (this.CollectionTypes[identifier] === 3) {
toRun(false, "edge");
}
else {
toRun(false, "document");
}
}
}.bind(this);
this.arangoDocumentStore.getCollectionInfo(identifier, callback, toRun);
}
if (this.CollectionTypes[identifier] === 3) {
return "edge";
else {
toRun(false, this.CollectionTypes[identifier]);
}
return "document";
},
collectionType: function (val) {

View File

@ -6,56 +6,42 @@ window.arangoDocument = Backbone.Collection.extend({
url: '/_api/document/',
model: arangoDocumentModel,
collectionInfo: {},
deleteEdge: function (colid, docid) {
var returnval = false;
try {
$.ajax({
cache: false,
type: 'DELETE',
async: false,
contentType: "application/json",
url: "/_api/edge/" + colid + "/" + docid,
success: function () {
returnval = true;
},
error: function () {
returnval = false;
}
});
}
catch (e) {
returnval = false;
}
return returnval;
deleteEdge: function (colid, docid, callback) {
$.ajax({
cache: false,
type: 'DELETE',
async: false,
contentType: "application/json",
url: "/_api/edge/" + colid + "/" + docid,
success: function () {
callback(false);
},
error: function () {
callback(true);
}
});
},
deleteDocument: function (colid, docid){
var returnval = false;
try {
$.ajax({
cache: false,
type: 'DELETE',
async: false,
contentType: "application/json",
url: "/_api/document/" + colid + "/" + docid,
success: function () {
returnval = true;
},
error: function () {
returnval = false;
}
});
}
catch (e) {
returnval = false;
}
return returnval;
deleteDocument: function (colid, docid, callback) {
$.ajax({
cache: false,
type: 'DELETE',
async: false,
contentType: "application/json",
url: "/_api/document/" + colid + "/" + docid,
success: function () {
callback(false);
},
error: function () {
callback(true);
}
});
},
addDocument: function (collectionID, key) {
var self = this;
self.createTypeDocument(collectionID, key);
},
createTypeEdge: function (collectionID, from, to, key) {
var result = false, newEdge;
createTypeEdge: function (collectionID, from, to, key, callback) {
var newEdge;
if (key) {
newEdge = JSON.stringify({
@ -69,22 +55,20 @@ window.arangoDocument = Backbone.Collection.extend({
$.ajax({
cache: false,
type: "POST",
async: false,
url: "/_api/edge?collection=" + collectionID + "&from=" + from + "&to=" + to,
data: newEdge,
contentType: "application/json",
processData: false,
success: function(data) {
result = data._id;
callback(false, data);
},
error: function(data) {
result = false;
callback(true, data);
}
});
return result;
},
createTypeDocument: function (collectionID, key) {
var result = false, newDocument;
createTypeDocument: function (collectionID, key, callback) {
var newDocument;
if (key) {
newDocument = JSON.stringify({
@ -98,21 +82,19 @@ window.arangoDocument = Backbone.Collection.extend({
$.ajax({
cache: false,
type: "POST",
async: false,
url: "/_api/document?collection=" + encodeURIComponent(collectionID),
data: newDocument,
contentType: "application/json",
processData: false,
success: function(data) {
result = data._id;
callback(false, data._id);
},
error: function(data) {
result = false;
callback(true, data._id);
}
});
return result;
},
getCollectionInfo: function (identifier) {
getCollectionInfo: function (identifier, callback, toRun) {
var self = this;
$.ajax({
@ -121,15 +103,14 @@ window.arangoDocument = Backbone.Collection.extend({
url: "/_api/collection/" + identifier + "?" + arangoHelper.getRandomToken(),
contentType: "application/json",
processData: false,
async: false,
success: function(data) {
self.collectionInfo = data;
callback(false, data, toRun);
},
error: function(data) {
callback(true, data, toRun);
}
});
return self.collectionInfo;
},
getEdge: function (colid, docid){
var result = false, self = this;

View File

@ -19,22 +19,11 @@
url: "/_api/gharial/" + encodeURIComponent(name) + "?dropCollections=true",
contentType: "application/json",
processData: true,
async: false,
success: function() {
arangoHelper.arangoNotification("Graph deleted.");
callback(true);
return true;
},
error: function(data) {
try {
var errorMsg = JSON.parse(data.responseText).errorMessage;
arangoHelper.arangoError("Graph", errorMsg);
}
catch (e) {
arangoHelper.arangoError("Graph", "Could not delete Graph.");
}
error: function() {
callback(false);
return false;
}
});
},

View File

@ -254,8 +254,17 @@
this.documentView.colid = colid;
this.documentView.docid = docid;
this.documentView.render();
var type = arangoHelper.collectionApiType(colid);
this.documentView.setType(type);
var callback = function(error, type) {
if (!error) {
this.documentView.setType(type);
}
else {
console.log("Error", "Could not fetch collection type");
}
}.bind(this);
arangoHelper.collectionApiType(colid, null, callback);
},
shell: function (initialized) {

View File

@ -53,6 +53,13 @@
editor: 0,
setType: function (type) {
if (type === 2) {
type = 'document';
}
else {
type = 'edge';
}
var result, type2;
if (type === 'edge') {
result = this.collection.getEdge(this.colid, this.docid);
@ -91,23 +98,7 @@
deleteDocument: function() {
var result;
if (this.type === 'document') {
result = this.collection.deleteDocument(this.colid, this.docid);
if (result === false) {
arangoHelper.arangoError('Document error:','Could not delete');
return;
}
}
else if (this.type === 'edge') {
result = this.collection.deleteEdge(this.colid, this.docid);
if (result === false) {
arangoHelper.arangoError('Edge error:', 'Could not delete');
return;
}
}
if (result === true) {
var successFunction = function() {
if (this.customView) {
this.customDeleteFunction();
}
@ -116,6 +107,29 @@
window.modalView.hide();
window.App.navigate(navigateTo, {trigger: true});
}
}.bind(this);
if (this.type === 'document') {
var callbackDoc = function(error) {
if (error) {
arangoHelper.arangoError('Error', 'Could not delete document');
}
else {
successFunction();
}
}.bind(this);
this.collection.deleteDocument(this.colid, this.docid, callbackDoc);
}
else if (this.type === 'edge') {
var callbackEdge = function(error) {
if (error) {
arangoHelper.arangoError('Edge error', 'Could not delete edge');
}
else {
successFunction();
}
}.bind(this);
this.collection.deleteEdge(this.colid, this.docid, callbackEdge);
}
},

View File

@ -46,12 +46,20 @@
setCollectionId : function (colid, page) {
this.collection.setCollection(colid);
this.collection.setPage(page);
var type = arangoHelper.collectionApiType(colid);
this.page = page;
this.type = type;
this.collection.getDocuments(this.getDocsCallback.bind(this));
this.collectionModel = this.collectionsStore.get(colid);
var callback = function(error, type) {
if (error) {
arangoHelper.arangoError("Error", "Could not get collection properties.");
}
else {
this.type = type;
this.collection.getDocuments(this.getDocsCallback.bind(this));
this.collectionModel = this.collectionsStore.get(colid);
}
}.bind(this);
arangoHelper.collectionApiType(colid, null, callback);
},
getDocsCallback: function(error) {
@ -459,102 +467,109 @@
addDocumentModal: function () {
var collid = window.location.hash.split("/")[1],
buttons = [], tableContent = [],
buttons = [], tableContent = [];
// second parameter is "true" to disable caching of collection type
doctype = arangoHelper.collectionApiType(collid, true);
if (doctype === 'edge') {
tableContent.push(
window.modalView.createTextEntry(
'new-edge-from-attr',
'_from',
'',
"document _id: document handle of the linked vertex (incoming relation)",
undefined,
false,
[
{
rule: Joi.string().required(),
msg: "No _from attribute given."
}
]
)
);
var callback = function(error, type) {
if (error) {
arangoHelper.arangoError("Error", "Could not fetch collection type");
}
else {
if (type === 'edge') {
tableContent.push(
window.modalView.createTextEntry(
'new-edge-to',
'_to',
'',
"document _id: document handle of the linked vertex (outgoing relation)",
undefined,
false,
[
{
rule: Joi.string().required(),
msg: "No _to attribute given."
}
]
)
);
tableContent.push(
window.modalView.createTextEntry(
'new-edge-from-attr',
'_from',
'',
"document _id: document handle of the linked vertex (incoming relation)",
undefined,
false,
[
{
rule: Joi.string().required(),
msg: "No _from attribute given."
}
]
)
);
tableContent.push(
window.modalView.createTextEntry(
'new-edge-key-attr',
'_key',
undefined,
"the edges unique key(optional attribute, leave empty for autogenerated key",
'is optional: leave empty for autogenerated key',
false,
[
{
rule: Joi.string().allow('').optional(),
msg: ""
}
]
)
);
tableContent.push(
window.modalView.createTextEntry(
'new-edge-to',
'_to',
'',
"document _id: document handle of the linked vertex (outgoing relation)",
undefined,
false,
[
{
rule: Joi.string().required(),
msg: "No _to attribute given."
}
]
)
);
buttons.push(
window.modalView.createSuccessButton('Create', this.addEdge.bind(this))
);
tableContent.push(
window.modalView.createTextEntry(
'new-edge-key-attr',
'_key',
undefined,
"the edges unique key(optional attribute, leave empty for autogenerated key",
'is optional: leave empty for autogenerated key',
false,
[
{
rule: Joi.string().allow('').optional(),
msg: ""
}
]
)
);
buttons.push(
window.modalView.createSuccessButton('Create', this.addEdge.bind(this))
);
window.modalView.show(
'modalTable.ejs',
'Create edge',
buttons,
tableContent
);
}
else {
tableContent.push(
window.modalView.createTextEntry(
'new-document-key-attr',
'_key',
undefined,
"the documents unique key(optional attribute, leave empty for autogenerated key",
'is optional: leave empty for autogenerated key',
false,
[
{
rule: Joi.string().allow('').optional(),
msg: ""
}
]
)
);
window.modalView.show(
'modalTable.ejs',
'Create edge',
buttons,
tableContent
);
}
else {
tableContent.push(
window.modalView.createTextEntry(
'new-document-key-attr',
'_key',
undefined,
"the documents unique key(optional attribute, leave empty for autogenerated key",
'is optional: leave empty for autogenerated key',
false,
[
{
rule: Joi.string().allow('').optional(),
msg: ""
}
]
)
);
buttons.push(
window.modalView.createSuccessButton('Create', this.addDocument.bind(this))
);
buttons.push(
window.modalView.createSuccessButton('Create', this.addDocument.bind(this))
);
window.modalView.show(
'modalTable.ejs',
'Create document',
buttons,
tableContent
);
}
window.modalView.show(
'modalTable.ejs',
'Create document',
buttons,
tableContent
);
}
}
}.bind(this);
arangoHelper.collectionApiType(collid, true, callback);
},
addEdge: function () {
@ -563,42 +578,44 @@
var to = $('.modal-body #new-edge-to').last().val();
var key = $('.modal-body #new-edge-key-attr').last().val();
var result;
if (key !== '' || key !== undefined) {
result = this.documentStore.createTypeEdge(collid, from, to, key);
}
else {
result = this.documentStore.createTypeEdge(collid, from, to);
}
if (result !== false) {
//$('#edgeCreateModal').modal('hide');
window.modalView.hide();
window.location.hash = "collection/"+result;
var callback = function(error, data) {
if (error) {
arangoHelper.arangoError('Error', 'Could not create edge');
}
else {
window.modalView.hide();
window.location.hash = "collection/" + data;
}
}.bind(this);
if (key !== '' || key !== undefined) {
this.documentStore.createTypeEdge(collid, from, to, key, callback);
}
//Error
else {
arangoHelper.arangoError('Edge error', 'Creation failed.');
this.documentStore.createTypeEdge(collid, from, to, null, callback);
}
},
addDocument: function() {
var collid = window.location.hash.split("/")[1];
var key = $('.modal-body #new-document-key-attr').last().val();
var result;
var callback = function(error, data) {
if (error) {
arangoHelper.arangoError('Error', 'Could not create document');
}
else {
window.modalView.hide();
window.location.hash = "collection/" + data;
}
}.bind(this);
if (key !== '' || key !== undefined) {
result = this.documentStore.createTypeDocument(collid, key);
this.documentStore.createTypeDocument(collid, key, callback);
}
else {
result = this.documentStore.createTypeDocument(collid);
}
//Success
if (result !== false) {
window.modalView.hide();
window.location.hash = "collection/" + result;
}
else {
arangoHelper.arangoError('Document error', 'Creation failed.');
this.documentStore.createTypeDocument(collid, null, callback);
}
},

View File

@ -11,10 +11,16 @@
};
var installCallback = function(result) {
var self = this;
if (result.error === false) {
this.collection.fetch({ async: false });
window.modalView.hide();
this.reload();
this.collection.fetch({
success: function() {
window.modalView.hide();
self.reload();
}
});
} else {
var res = result;
if (result.hasOwnProperty("responseJSON")) {

View File

@ -105,8 +105,9 @@
}
else {
window.modalView.hide();
arangoHelper.arangoError("Graph", "Could not delete Graph.");
}
};
}.bind(this);
this.collection.dropAndDeleteGraph(name, callback);
}
@ -187,39 +188,42 @@
},
render: function() {
var self = this;
this.collection.fetch({
async: false
});
this.collection.sort();
success: function() {
self.collection.sort();
$(this.el).html(this.template.render({
graphs: this.collection,
searchString : ''
}));
$(self.el).html(self.template.render({
graphs: self.collection,
searchString : ''
}));
if (this.dropdownVisible === true) {
$('#graphManagementDropdown2').show();
$('#graphSortDesc').attr('checked', this.collection.sortOptions.desc);
$('#graphManagementToggle').toggleClass('activated');
$('#graphManagementDropdown').show();
}
this.events["click .tableRow"] = this.showHideDefinition.bind(this);
this.events['change tr[id*="newEdgeDefinitions"]'] = this.setFromAndTo.bind(this);
this.events["click .graphViewer-icon-button"] = this.addRemoveDefinition.bind(this);
this.events["click #graphTab a"] = this.toggleTab.bind(this);
this.events["click .createExampleGraphs"] = this.createExampleGraphs.bind(this);
this.events["focusout .select2-search-field input"] = function(e){
if ($('.select2-drop').is(':visible')) {
if (!$('#select2-search-field input').is(':focus')) {
window.setTimeout(function() {
$(e.currentTarget).parent().parent().parent().select2('close');
}, 80);
if (self.dropdownVisible === true) {
$('#graphManagementDropdown2').show();
$('#graphSortDesc').attr('checked', self.collection.sortOptions.desc);
$('#graphManagementToggle').toggleClass('activated');
$('#graphManagementDropdown').show();
}
}
}.bind(this);
arangoHelper.setCheckboxStatus("#graphManagementDropdown");
self.events["click .tableRow"] = self.showHideDefinition.bind(self);
self.events['change tr[id*="newEdgeDefinitions"]'] = self.setFromAndTo.bind(self);
self.events["click .graphViewer-icon-button"] = self.addRemoveDefinition.bind(self);
self.events["click #graphTab a"] = self.toggleTab.bind(self);
self.events["click .createExampleGraphs"] = self.createExampleGraphs.bind(self);
self.events["focusout .select2-search-field input"] = function(e){
if ($('.select2-drop').is(':visible')) {
if (!$('#select2-search-field input').is(':focus')) {
window.setTimeout(function() {
$(e.currentTarget).parent().parent().parent().select2('close');
}, 80);
}
}
}.bind(self);
arangoHelper.setCheckboxStatus("#graphManagementDropdown");
}
});
return this;
},