Question

I have the following code:

function initializeUI(){
    //More code here

    models.player.addEventListener('change:context', contextChanged);

    //More code here
}

var timerId = null;
function contextChanged() {
    models.player.load('context').done(function (player) {
        if (player.context.uri == tempPlaylist.uri) {
            //User started playing this context. Start timer.
            timerId = setInterval(updateTimerView, 1000);
        }
        else {
            //User is playing a different context. Delete temp playlist and timer.
            models.Playlist.removeTemporary(tempPlaylist);
            clearInterval(timerId);
        }
    });
}

function updateTimerView(){
    //UI related code goes here
}

I would assume that change:context would only be fired when the context (in my case, tempPlaylist) is changed, but it turns out that it's also being fired when the track is changed, played, or paused. This can't be the desired functionality... otherwise it would defeat the purpose of having the change:track and change:playing events.

This is causing a lot of trouble for me mainly because when the track is changed, played, or paused, contextChanged is called, but player.context.uri hasn't changed, so more and more timers keep getting added. What am I missing here?

Was it helpful?

Solution

I agree, looks like a bug. I'd probably change it to something like this

require(['$api/models'], function(models) {

    models.player.addEventListener('change:context', contextChanged);

    var last_context=null;
    function contextChanged(e) {
        if(last_context != e.oldValue.uri) {
            last_context = e.oldValue.uri;
            console.log('hola, new context uri - ' + last_context);
        }
        else {
            console.log('faux context change');
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top