0

I am forced to use data from within the getJSON callback function outside of that function. Have a look:

$('#stars').raty({
        score: function() {
            var $value = 0;
            var $id = <?=$id?>;
            $.getJSON("getuserrating.php?id=" + $id, function(data) {
                $.each(data, function(key, val) {
                    $value = val;
                });
            });
            return $value;
        },
    });

This is what I tried but it failed, the $value is still set to 0, although inside the callback it's definitely set to an actual value. I know why it fails, because the AJAX request is send asynchronously. The problem is, the correct way, doing everything inside the callback, is not possible. As you can see I need to retrieve the JSON object within the raty (a plugin) setup. I would just use $.ajax() and set that to synchronous but the documentation marks this as deprecated for 1.8. I'd rather not introduce a solution that I know will be deprecated in the future.

Is there any way at all to do this? Maybe I just don't see the forest for the trees and there's an easy solution right before me. Thanks in advance :)

3
  • 1
    This question has been already asked millions of times. Please use the corresponding search engines to find the duplicate questions. But to simplify your efforts, no, there's no way to achieve that other than using the async: false switch which obviously is not something that I would recommend you doing. Just get accustomed to asynchronous programming and throw into the trash libraries and plugins that do not support this paradigm and replace them with libraries that do support it and provide you the possibility to work with callbacks. Commented Jun 19, 2012 at 18:40
  • Nope, there is not. Have a look at the documentation if the plugin and see whether it provides a way to use asynchronous functions as values. If not, there is nothing you can do, despite suggesting this to the developer. Commented Jun 19, 2012 at 18:40
  • @DarinDimitrov I specifically mentioned that I know what's causing the problem but that I'm looking for a workaround other than async: false. The "millions" of questions you refer to were all solved by making the OP work within the callback. As you might have noticed, my problem was that I didn't see a way to solve my matter within the callback itself. Hence, those millions of answers did not actually answer my specific question. Suggesting to trash a widely spread plugin that does its main job best does not seem very supportive either. Commented Jun 19, 2012 at 21:52

2 Answers 2

4

Approach is backwards if you need to use ajax to get the score.

Make the ajax call first, then pass the value to score within the ajax success

$.getJSON("getuserrating.php?id=" + $id, function(data) {
          /* pseudo code here since I don't know the plugin and it's data requirments*/
         var score= data.score;
         $('#stars').raty({
             score: score
         });      
});

EDIT: you can still pass the data into a score function this way also

Sign up to request clarification or add additional context in comments.

4 Comments

I thought the same, as long as a primitive value for score is acceptable.
Thanks a lot for the input. This looked very promising and exactly like a solution that I had stupidly overlooked but I'm running into problems. When trying to call the raty function within the callback "$('#stars').raty is not a function" errors are triggered. This seems to suggest the raty plugin has not been loaded yet, but that doesn't make a lot of sense. Any ideas?
are you loading jquery more than once in page, if so, any plugins attached to first load get wiped out. Also make sure options passed to plugin are valid code
thanks :) I'm still researching where exactly it happens but you are right, the plugin gets wiped. Your solution works fine in a minimal test environment.
0

Maybe this (taken from jquery getJSON api doc http://api.jquery.com/jQuery.getJSON/) could work:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });

Just assign another raty attribute instead of returning.

1 Comment

.success .error and .complete are depreciated and replaced by .done .fail and .always, the documentation seems to be out of date on that page. api.jquery.com/jQuery.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.