admin管理员组文章数量:1435859
I don't understand why the click
events are firing ok but not the timeupdate
one.
No event is firing from <audio>
, I tried with canplay, play, pause, timeupdate, ...
If I display the controls in the template a click .audio
event will fire if I click on the player though.
var PlayerView = Backbone.View.extend({
tagName : 'div',
template: _.template($('#player-template').html()),
initialize: function() {
this.$el.html(this.template(this.model.toJSON()));
}
events: {
'timeupdate .audio': 'onTimeUpdate',
'click .btn_play': 'onClickPlay',
'click .btn_pause': 'onClickPause'
},
onTimeUpdate: function () {
console.log('player ' + this.model.get('id') + ' : event = onTimeUpdate');
},
onClickPlay: function () {
console.log('player ' + this.model.get('id') + ' : event = onClickPlay');
},
onClickPause: function () {
console.log('player ' + this.model.get('id') + ' : event = onClickPause');
},
});
$(function(){
var model = new PlayerModel({'id' : 1});
var view = new PlayerView({'model' : model});
$("#players").append(view.el);
})
The template :
<script type="text/template" id="player-template">
<div id="player<%= id %>" class="player">
<audio class="audio">
<source src="<%= track_url %>" type="audio/mp3" />
</audio>
<div class="control">
<button class="btn_play">Play</button>
<button class="btn_pause">Pause</button>
</div>
</div>
</script>
I don't understand why the click
events are firing ok but not the timeupdate
one.
No event is firing from <audio>
, I tried with canplay, play, pause, timeupdate, ...
If I display the controls in the template a click .audio
event will fire if I click on the player though.
var PlayerView = Backbone.View.extend({
tagName : 'div',
template: _.template($('#player-template').html()),
initialize: function() {
this.$el.html(this.template(this.model.toJSON()));
}
events: {
'timeupdate .audio': 'onTimeUpdate',
'click .btn_play': 'onClickPlay',
'click .btn_pause': 'onClickPause'
},
onTimeUpdate: function () {
console.log('player ' + this.model.get('id') + ' : event = onTimeUpdate');
},
onClickPlay: function () {
console.log('player ' + this.model.get('id') + ' : event = onClickPlay');
},
onClickPause: function () {
console.log('player ' + this.model.get('id') + ' : event = onClickPause');
},
});
$(function(){
var model = new PlayerModel({'id' : 1});
var view = new PlayerView({'model' : model});
$("#players").append(view.el);
})
The template :
<script type="text/template" id="player-template">
<div id="player<%= id %>" class="player">
<audio class="audio">
<source src="<%= track_url %>" type="audio/mp3" />
</audio>
<div class="control">
<button class="btn_play">Play</button>
<button class="btn_pause">Pause</button>
</div>
</div>
</script>
Share
Improve this question
asked Jul 12, 2012 at 23:00
MatthieuMatthieu
2,2892 gold badges15 silver badges11 bronze badges
1 Answer
Reset to default 11Backbone internally converts the events object and binds the events by delegation. This means that
events: {
'timeupdate .audio': 'onTimeUpdate',
'click .btn_play': 'onClickPlay'
}
ends as
this.$el.delegate('.audio','timeupdate', this.onTimeUpdate);
this.$el.delegate('.btn_play','click', this.onClickPlay);
But there's a catch:
Uses event delegation for efficiency. [...] This only works for delegate-able events: not focus, blur, and not change, submit, and reset in Internet Explorer.
And it would appear that audio events are not delegate-able.
However, setting directly the callbacks does work, for example you could replace your initialize function by
initialize: function() {
this.$el.html(this.template(this.model.toJSON()));
_.bindAll(this,'onTimeUpdate');
this.$('.audio').on('timeupdate', this.onTimeUpdate);
}
本文标签: javascriptWhy audio events are not firing with BackboneJS but others areStack Overflow
版权声明:本文标题:javascript - Why audio events are not firing with BackboneJS but others are? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744508099a2609707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论