I have this code it works perfectly:
$.getJSON('/admin/get_acronyms', function(data) {
//load JSON
$.each(data, function(key, val) {
if(key === 'error') {
alert(val);
} else {
//Update typeahead source
autocomplete.data('typeahead').source = val;
}
});
});
And I want to minimize it inside this function for code reusing:
var getAndAppendJSON = function(url) {
var result;
$.getJSON(url, function(data) {
$.each(data, function(key, val) {
return val;
});
});
};
var arr = getAndAppendJSON('/admin/get_acronyms');
autocomplete.data('typeahead').source = arr;
But this function doesn't work :(.
JS Console shows me that var arr is undefinied
Anyone knows why? Thanks in advance.