Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js BackboneJS如何在视图中加载Youtube Iframe_Backbone.js_Youtube_Youtube Api - Fatal编程技术网

Backbone.js BackboneJS如何在视图中加载Youtube Iframe

Backbone.js BackboneJS如何在视图中加载Youtube Iframe,backbone.js,youtube,youtube-api,Backbone.js,Youtube,Youtube Api,我有一个主干应用程序,其中有一个视图,我想在其中显示youtube播放器: 因此,在我看来,在afterRender-我做了以下工作: afterRender: function() { var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag =

我有一个主干应用程序,其中有一个视图,我想在其中显示youtube播放器:

因此,在我看来,在
afterRender
-我做了以下工作:

afterRender: function() {
              var tag = document.createElement('script');

              tag.src = "https://www.youtube.com/iframe_api";
              var firstScriptTag = document.getElementsByTagName('script')[0];
              firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


              var player;
              function onYouTubeIframeAPIReady() {
                player = new YT.Player('vid', {
                  height: '390',
                  width: '640',
                  videoId: '',
                  events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                  }
                });
              }

              function onPlayerReady(event) {
                event.target.playVideo();
              }

              var done = false;
              function onPlayerStateChange(event) {
                if (event.data == YT.PlayerState.PLAYING && !done) {
                  setTimeout(stopVideo, 6000);
                  done = true;
                }
              }
              function stopVideo() {
                player.stopVideo();
              }
}
但它没有呈现任何东西,但我也没有得到错误,在我的控制台中,我可以看到Youtube API已加载。。。那么我做错了什么


请帮忙

请参见此相关问题:

我认为问题在于,您的
onyoutubeiframeapiredy
函数需要全局声明,而不是嵌套在另一个函数中。(在代码中,它仅在
afterRender
方法的范围内定义。)

通过更改

function onYouTubeIframeAPIReady() {
 // ...
}
进入

它现在起作用了!在这里找到了线索

window.onYouTubeIframeAPIReady = function() {
 // ...
}