Search code examples
javascriptember.jsember-clisoundcloud

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);
        }
    }
}

How can i access to the favorite index of my array favorites ?

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

In the code above i pass it as "0" but i need to pass this correct index of the favorite song i click as setAsFavorite


Solution

  • Did some minor refacto on your code. Added index of the favorite of favorites array. Also provided a way of getting next song instantly

    actions: {
            setAsFavorite: function (favorite) {
                var favorites = this.get('model');
                var player = this.get('player');
    
                if (Ember.isPresent(favorite)) {
                  player.set('favorites', favorites);
                  player.send('selectFavorite', favorite, favorites.indexOf(favorite)); 
                  // for next song 
                 //  player.send('selectFavorite', favorite, favorites.nextObject(favorite)); 
                } 
            }
        }