Skip to content

Instantly share code, notes, and snippets.

@patarapolw
Created May 24, 2018 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patarapolw/9f0948276a198fc8b5c6be061138fe5a to your computer and use it in GitHub Desktop.
Save patarapolw/9f0948276a198fc8b5c6be061138fe5a to your computer and use it in GitHub Desktop.
Getting asynchronous POST to the top level Return (jQuery contextMenu Asynchronous)
$('#vocab').contextMenu({
selector: ".entry",
build: function($trigger, e) {
e.preventDefault();
function buildMenu(buildMenuCallback){
$.post('/vocabInLearning', { vocab: $('a', this).text() }, function(data) {
var isLearnt = (data === '1');
buildMenuCallback({
items: {
addToLearning: {
name: "Add vocab to learning",
visible: function(key, opt){
return !isLearnt;
},
callback: function(key, opt){
$.post('/addVocabToLearning', { vocab: $('a', this).text() }, function(data) {
if(data !== '1'){
alert('Adding vocab to learning failed.');
} else {
alert('Added vocab to learning');
}
});
}
},
removeFromLearning: {
name: "Remove vocab from learning",
visible: function(key, opt){
return isLearnt;
},
callback: function(key, opt){
$.post('/removeVocabFromLearning', { vocab: $('a', this).text() }, function(data) {
if(data !== '1'){
alert('Remove vocab from learning failed.');
} else {
alert('Removed vocab from learning');
}
});
}
}
}
});
});
}
$trigger.data('buildMenu', buildMenu);
return $trigger.data('buildMenu')();
}
});
$('#sentences').contextMenu({
selector: ".entry",
build: function($trigger, e) {
e.preventDefault();
function buildMenu(){
$.post('/sentenceInLearning', { vocab: $('a', this).text() }, function(data) {
var isLearnt = (data === '1');
return {
items: {
addToLearning: {
name: "Add sentence to learning",
visible: function(key, opt){
return !isLearnt;
},
callback: function(key, opt){
$.post('/addSentenceToLearning', { vocab: $('a', this).text() }, function(data) {
if(data !== '1'){
alert('Adding sentence to learning failed.');
} else {
alert('Added sentence to learning');
}
});
}
},
removeFromLearning: {
name: "Remove sentence from learning",
visible: function(key, opt){
return isLearnt;
},
callback: function(key, opt){
$.post('/removeSentenceFromLearning', { vocab: $('a', this).text() }, function(data) {
if(data !== '1'){
alert('Remove sentence from learning failed.');
} else {
alert('Removed sentence from learning');
}
});
}
}
}
}
});
}
$trigger.data('buildMenu', buildMenu);
return $trigger.data('buildMenu')();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment