开发者

jQuery call synchronously

开发者 https://www.devze.com 2023-03-16 04:02 出处:网络
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

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

Ben


Try 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;

                               }
                     });
                   }

                }
            });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消