dhruvaray / backbone-associations

Create object hierarchies with Backbone models; Respond to hierarchy changes using regular Backbone events.
http://dhruvaray.github.io/backbone-associations/
MIT License
492 stars 75 forks source link

preventing relations key from being stored on AssociatedModel #127

Closed gr2m closed 10 years ago

gr2m commented 10 years ago

I've a Meeting & a Minute model, as well as a MinutesCollection. Here's my setup, everything works great so far.

var Model = BackboneAssociations.AssociatedModel.extend({
  type: 'meeting',
  relations: [
    {
      type: BackboneAssociations.Many,
      key: 'minutes',
      collectionType: MinutesCollection,
      relatedModel: Minute
    }
  ]
});

On save, I'd not like a meeting model to store the minutes as a property, as I store them separately. I achieve that currently by overiding the toJSON() method like so:

  toJSON: function() {
    var attributes = _.clone(this.attributes);
    delete attributes.minutes;
    return attributes;
  },

That works, but I just want to make double sure that this is the right way to do it and that there are no side effects

dhruvaray commented 10 years ago

Nothing wrong with what you are doing, but you can do it far more easily like this


var MeetingModel = Backbone.AssociatedModel.extend({
  relations: [
    {
      type: Backbone.Many,
      key: 'minutes',
      collectionType: MinutesCollection,
      isTransient : true // Will not serialize `minutes` on `save`
    }
  ]
});
gr2m commented 10 years ago

that's what I was looking for, most excellent! Thanks a lot, Dhruva!