Search code examples
backbone.jsdom-eventshtml5-audio

Why <HTMLAudioElement> has no method 'set'


My code is below, when I run it, it shows error - <HTMLAudioElement> has no method 'set'. Why it''s happening? I've bound my functions to model, but it seems to work wrongly:

var Player = Backbone.Model.extend({
    initialize: function(){         
        _.bind(this.ontimeupdate, this);
        _.bind(this.onprogress, this);
    },      
    setAudio: function(ogglink, mp3link, ontimeupdate){
        var el = document.createElement("audio");           
        el.addEventListener('timeupdate', this.ontimeupdate);
        this.audiotag = el;
    },      
    ontimeupdate: function() {
        this.set("curtime", this.currentTime);
    }
});

Solution

  • Unlike bindAll (which you should be using as you make several binds), bind only returns the binded function (equivalent to ECMA's bind method). So...

    this.ontimeupdate = _.bind(this.ontimeupdate, this);
    this.onprogress = _.bind(this.onprogress, this);
    

    Or

    _.bindAll(this);
    

    Or

    _.bindAll(this, 'ontimeupdate', 'onprogress');