我正在尝试在模板中制作youtube iframe,并从多个页面调用该模板。我可以在首页加载时加载视频(无论我从哪里调用player
模板),但只要我从一个具有player
模板的页面导航到另一个页面,就会删除iframe dom并删除再次加载,直到刷新。
我最初的假设是因为onYouTubeIframeAPIReady
只被调用一次而且铁路由器正在兑现dom元素。
模板:
<template name="player">
<div class="template-banner">
<div class="video-container" id="player">
</div>
</div>
</template>
模板JS
downloadIFrameAPI = _.once(->
Meteor.startup ->
$.getScript '//www.youtube.com/iframe_api'
return
return
)
Player =
create: (playerId, videoTemplate, video) ->
window.onYouTubeIframeAPIReady = ->
console.log 'creating player...'
window.player = new (YT.Player)(playerId,
width: '560'
height: '349'
events:
onReady: ->
console.log 'Player Loaded'
Player.loadVideo(video)
)
videoTemplate.rendered = onYouTubeIframeAPIReady
downloadIFrameAPI()
loadVideo: (video) ->
player.loadVideoByUrl
mediaContentUrl: video
Template.player.onRendered ->
Player.create 'player', Template.player, this.data.video