jQuery call synchronously
I'm combining 2 jQuery plugins, Fancybox and Embedly (like jQuery oembed).
In the Fancybox onStart event I get the url of link, make a call to the oembed service and push the response (a video) back into the lightbox.
Well at least that's what I want to happen :) I'm getting a response back from the oembed provider but I think t开发者_JAVA百科he onStart function has already completed by the time this happens.
The code:
$('a').fancybox({
type: 'html',
onStart: function(selectedArray, selectedIndex, selectedOpts) {
var url = selectedArray[selectedIndex].href;
$.embedly(url, {
success: function (oembed, dict) {
selectedOpts.content = oembed.html;
}
});
}
});
It seems I need to process the call synchronously?
Thanks
BenTry this
$('a').each(function(i, a){
var url = a.href, $a = $(a);
$.embedly(url, {
success: function (oembed, dict) {
$a.fancybox({
type: 'html',
onStart: function(selectedArray, selectedIndex, selectedOpts) {
selectedOpts.content = oembed.html;
}
});
}
}
});
});
精彩评论