0

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?

4

1 回答 1

1

我同意,看起来像一个错误。我可能会把它改成这样

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');
        }
    }
});
于 2013-12-26T18:45:28.150 回答