2

I have this code

var children;
$.ajax({
    url: Routing.generate('snmp_ajax_get_children', {dev: root}),
    async: true, type: "GET"
}).done(function(data) {
    var children = Array(); 
    for(var i in data) {
        children[i] = data[i].split('|');
        for (var j in data[i]) {
            children[i][j] = $.trim(data[i][j]);
        }
    }
    localStorage.setItem('children', children);
});
children = localStorage.getItem(children);
localStorage.removeItem('children');

I use localStorage (ugly, i know) to get data from callback, because any other approach wasn't work for me (i don't know why), any suggestion?

Neka
  • 1,574
  • 4
  • 22
  • 36

2 Answers2

4

Since you work with async ajax, you can't request the result of the response until done is really done. To achieve something like this, you could do:

// receiving data
function getData( callback ) {
    $.ajax({
        url: Routing.generate('snmp_ajax_get_children', {dev: root}),
        async: true, type: "GET"
    }).done(function(data) {
        // is async, so it takes some time until this is triggered...
        // I don't know your response but I think children should be 
        // an object: 
        // var children = {};
        var children = Array(); 
        for(var i in data) {
            children[i] = data[i].split('|');
            for (var j in data[i]) {
                children[i][j] = $.trim(data[i][j]);
            }
        }
        // calling your data handler with the data
        callback( children );
    });
}

// your data handler
function handleData( data ) {
    // do whatever

} 


// call the action, setting the callback
getData ( handleData );
axel.michel
  • 5,764
  • 1
  • 15
  • 25
  • Set default value to callback "callback = callback || null" and test if callback == null before calling it, then the callback is optionnal. – sdespont Feb 14 '13 at 07:34
  • @sdespont for sure, for a real project, you should enhance this example by parameters for url, data, type etc. (or using a kind of settings object for the ajax request and callback). – axel.michel Feb 14 '13 at 07:44
  • @axel.michel can you add more details to your comment? Because this code for the real intranet project for provider company. – Neka Feb 14 '13 at 07:56
  • @sdespont this is "in delelopment" code. Normally i will use async: false for this piece of code. – Neka Feb 14 '13 at 07:58
  • @Neka I put together a small example for an abstract jQuery AJAX Wrapper: http://pastie.org/6161223 – axel.michel Feb 14 '13 at 08:37
0

Because 'children' variable defines 2 times.

as you know, function has its own context for local variables. and if you define a variable with var, it belongs to function context (even if you use same name...)

}).done(function(data) {
    var children = Array(); 
    // ...

In this situation, children variable will closed when end of callback. let's remove 2nd var syntax for accessing outer 'children'.

}).done(function(data) {
    children = Array(); 
    // ...
cwdoh
  • 196
  • 10