views:

103

answers:

1

Inside of a jQuery plugin I made I have:

$.getJSON(base_url,{
    agenda_id:defaults.id,
    action:defaults.action+defaults.type,
    output:defaults.output
},function(json){
    return json;
});

And in a separate JS file (yes, it comes after the plugin):

json = $('#agenda-live-preview').agenda({action:'get',type:'agenda',output:'json'});
alert(json[0].agenda_id);

If i do the above $.getJSON and put an alert inside of the $.getJSON it works and returns "3", which is correct. If I do it like the json=$('#agenda-live-preview').agenda(...)... it returns undefined.

My JSON is valid, and the json[0].agenda_id is correct also, I know it's in a callback, so how do I get the stuff inside of a callback in a function return?

+1  A: 

Because an AJAX request is asynchronous by default, the alert() is running before the AJAX request is received, and the json variable has therefore not been assigned a value.

Whatever functionality you want (the alert, for example) will need to be in the callback to the AJAX request, or will need to be in a function called from within the callback, or perhaps called using .ajaxSuccess().

Or perhaps you could pass a function as a parameter to your plugin, and have the $.getJSON() callback call it.


EDIT:

Example of passing in a callback to run upon successful $.getJSON() request:

$.fn.agenda = function(opts) {

    var defaults = {...} // your defaults

    $.extend(defaults, opts); // Extend your defaults with the opts received

    $.getJSON(base_url,{
        agenda_id:defaults.id,
        action:defaults.action+defaults.type,
        output:defaults.output
    },function(json){
        defaults.success.call(this,json);  // Call the 'success' method passing in the json received
    });
};

$('#agenda-live-preview').agenda({
    action:'get',
    type:'agenda',
    output:'json',
    success:function(data) {  // Pass a function to the plugin's 'success' property
        alert(data[0].agenda_id);
        alert( $(this).selector );
    }
});
patrick dw
Yeah, I knew it's asyncronous i just couldn't think of anyway of doing it. Could you give me an example of that last suggestion you had with passing a function in?
Oscar Godson
@Oscar - Sure. Not sure how your plugin is structured, but I'll update my answer with a simple example.
patrick dw
Awesome! thank you so much. I never knew how to do callbacks, that worked perfectly!
Oscar Godson
@Oscar - You're welcome. :o)
patrick dw
Actually... one small question... how do I return the selector used so, agenda-live-preview rather than the JSON/XML/HTML when using $(this) in the callback? e.g. inside of the success putting: alert($(this).attr('id')); should say 'agenda-live-preview', not 3?
Oscar Godson
@Oscar - I'm not very familiar with javascript's `.call()` method, but try this in the plugin to call the callback: `defaults.success.call(this,json);` Then you should be able to do `alert( $(this).selector );` I'm pretty sure that's right. I updated my answer. Let me know how it goes.
patrick dw
you're awesome man! I did not know about the call method either. Ill give you another +1 and a "correct answer" here (just copy and paste your answer above!): http://stackoverflow.com/questions/3048190/how-to-return-this
Oscar Godson
@Oscar - Already placed an answer in the other question. Hope it helps. :o)
patrick dw