jeudi 15 octobre 2015

Accessing model index and send to controller with Ember

I am building a Music Player Component with my favorites songs by SoundCloud API and Ember Cli

The Music Player is playing the audio anytime i select a song but i am trying now to play the nextFavorite once the the current song is finished.

I need to access to the favorite model index of my favorites to get the next Favorite

Here the Music controller

player: Ember.computed.alias('controllers.player')

actions: {
    setAsFavorite: function (favorite) {
        var favorites, favorite
        favorites = this.get('model');
        if (favorite != null) {
            this.get('player').set('favorites', favorites);
        }
        if (favorite != null) {
            return this.get('player').send('selectFavorite', favorite, 0);
        }
    }
}

Look the console

enter image description here

How can i access to the favorite index of my favorites array in order to send it to my Player Controller?

return this.get('player').send('selectFavorite', favorite, 0);

In the code above i pass it as "0" but i need to pass the correct index of the favorite song for my nextFavorite

Player Controller with 'selectFavorite'

sortedFavorites: Ember.computed.alias('favorites')

selectFavorite: function(favorite, index, play) {
    var nextFavorite, prevFavorite, self, favoritePath;
    self = this;
    if (play == null) {
        play = true;
    }
    // self.get('favorite').set('playingFavorite', false);
    // self.set('isBuffering', true);
    favorite.set('playingFavorite', true);
    favoritePath = favorite.get('uri');
    prevFavorite = this.get('currentFavoriteObject');
    if (prevFavorite != null) {
        prevFavorite.destruct();
    }
    self.set('currentFavorite', favorite);
    if (!this.get('externalPlay')) {
        index = index + 1;
        nextFavorite = this.get('sortedFavorites').nextObject(index, favorite);
        self.set('nextFavorite', nextFavorite);
    }
    return SC.stream(favoritePath, {
        whileplaying: function() {
            return self.set('currentFavoritePosition', this.position);
        },
        onbufferchange: function() {
            return self.set('isBuffering', this.isBuffering);
        },
        onfinish: function() {
            self.set('isPlaying', false);
            if (self.get('nextFavorite') != null) {
                return self.send('selectFavorite', self.get('nextFavorite'), index);
            }
        }
    }, function(sound) {
        self.set('currentFavoriteObject', sound);
        self.set('isPlaying', true);
        sound.play();
        if (!play) {
            sound.pause();
            return self.set('isPlaying', false);
        }
    });
}

The incorrect favorite index

        index = index + 1;
        nextFavorite = this.get('sortedFavorites').nextObject(index, favorite);

does not let me to access to the next Favorite of my current Favorite

Console for the Player Component

enter image description here

The Player template

{{soundcloud-player favorite=currentFavorite sound=currentFavoriteObject position=currentFavoritePosition isPlaying=isPlaying isBuffering=isBuffering}}




Aucun commentaire:

Enregistrer un commentaire