2

In my application, the "Song" model holds a few strictly client-side attributes, such as the 'sound' attribute, which holds song data requested from SoundCloud the first time a song is played. Similarly, an attribute 'playing' makes it simple to determine if the song model is currently playing when rendering views.

Thanks to the question "Exclude model properties when syncing (Backbone.js)" I was able to blacklist the attributes when syncing to the server, but the problem arises when I call fetch() on a Song collection as the blacklisted attributes are overwritten.

I've tried overwriting the parse method (recommended here: How can I persist custom attributes over a collection fetch) for the Song collection, but to no avail.

parse: function(response) {
      // ensure that the value of the black-listed attributes for any of the models
      // is persisted into the model in the new collection
      this.each(function(song) {
        var newSong = _.find(response, function(responseSong) {
          return song.get('_id') == responseSong._id.$oid;
        });
        _.each(Song.blacklist, function(attr) {
          newSong[attr] = song[attr];
        });
      });
      return response;
    }

A console.log revealed that the response is actually set correctly, but after the collection fetch the song models do not seem to reflect the response. Anyhow, the parse method doesn't seem to be the right thing to do anyway, so I'm wondering if anyone knows a better way to tackle the problem?

Thanks, Anton

Community
  • 1
  • 1
Anton Abilov
  • 343
  • 3
  • 11
  • Hmm, but it really isn't about tracking something, the song is "loaded" from soundcloud and for that session I feel it would be reduntant to reload it. The data surely belongs to the model, it's just that it shouldn't be on the server-side. – Anton Abilov Jun 02 '14 at 20:40
  • Well, the Song models are in a Songs collection, but I don't have one explicitly for the ones loaded from Soundcloud. – Anton Abilov Jun 03 '14 at 06:26

0 Answers0