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>
Backbone internally converts the events object and binds the events by delegation. This means that
ends as
But there's a catch:
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