inaes-tic/mbc-caspa — pull request #210 by pardo-bsso (2014-01-27 19:01:22+00:00)
Title: EditView: make savePlaylist() work with the reverse relationship from Piece to Playlist.

Sorry everyone for this. All I can say is that I am grateful we don't
have yet means to punch someone over the net.

In models/Media.js we set up the reverse relation from Piece to Playlist
so when a new Piece is created somewhere else it doesn't end up tied to
a random playlist on the other browsers as it did before.

This works like a charm for an already existing playlist.

However, if we are about to save a new playlist it doesn't have an _id
yet, and so all the pieces are left with a null parentPlaylist.
This ultimately leads to them and the original playlist not being saved.
Well, it's worse than that. They are saved but parentPlaylist is null and on
the playlist 'pieces' is an array of null.

So we first save the playlist without pieces, the server emits a 'create'
event and notifies everyone.

After that, we have a playlist with an id. Then we re-add all the pieces and
save (again). This will correctly create the pieces and relations here and on
the other browsers.

Why not just slap an uuid.v4() like the middleware would do anyway?
If we do that the call to isNew() returns false. Here it's no biggie but the
fine oiled machine that our Backbone-* stack is uses it internally, and I don't
really feel like messing with that. One of the most visible consecuences of it
is that the server broadcasts an 'update' event instead of a 'create', so the
other clients won't see the new playlist.

Nowadays we are only creating playlists from here but all this sholud really go
inside the model. But there we don't always have a Universe (for new playlists
this.collection is undefined)