samedi 21 mars 2015

Replace an object in Array without rerendering twice

I've got an Object-Array containing songs. Upon clickon on one I'd like to replace that one in the list with the current track (another property). Since it's just an ordinary Ember.Array I can't use replaceContent. I remove the element and add it back like this:



playlist.removeAt(index, 1)
playlist.insertAt(index, self.get("currentTrack"))


However this re-renders the template twice. Since this looks ugly I'm looking for a way to replace the object in one go or prevent the re-rendering.


The template:



{{#each song in playlist}}
<div class="song" {{action "changeTrack" song}}>
<img {{bind-attr src=song.artwork_url}} />
</div>


The controller:



export default Ember.ArrayController.extend({

playlist: null,
currentTrack: null,

actions: {
changeTrack: function(track) {
var playlist = self.get("playlist")

playlist.forEach(function(song, index){
if (song.id === track.id){
playlist.removeAt(index, 1)
playlist.insertAt(index, self.get("currentTrack"))
//playlist.replaceContent(index, 1, [ self.get("currentTrack") ]) // doesn't work
}
}
}
}
})




Aucun commentaire:

Enregistrer un commentaire