mardi 27 janvier 2015

Update template after model update

I'm trying to create a track list page. On the page we have songs with their name, artist and such. When the user votes for a song(up/down) the number of votes should be updated and the list should be re-filtered so that the song with the most votes is the first in the list.


Until now this was done by the server. But we are trying to avoid that get request so that only one post for the vote has to be done.


Getting the amount of votes to update was very simple:



route.store.find('Track', track_id).then(function (track) {
track.set('votes', track.get('votes') + 1);
});


But after searching Google and the Ember docs for a while there seems to be not a lot about the sorting of results so that when I update a property on the a model that the order will also update on the template.


Model:



App.Track = DS.Model.extend({
name: DS.attr('string'),
artist: DS.attr('string'),
picture: DS.attr('string'),
votes: DS.attr('number'),

voted: DS.attr('boolean'),
vote_type: DS.attr('boolean')
});


Controller:



App.TracklistingController = Ember.ArrayController.extend({
sortProperties: ['votes'],
sortAscending: false
});


I use the following to get the tracks in the current list:



return route.store.all('Track');


This is the first project we are doing in Ember so I'm just a newb in the world of Ember and have no idea what is going wrong here.





Aucun commentaire:

Enregistrer un commentaire