documentcloud/backbone — pull request #808 by iros (2011-12-22 17:52:46+00:00)
Title: Collection.add now takes a 'unique' option that will guarantee model uniqueness

Currently, when adding a model to a collection, the only way uniqueness is guaranteed is by checking its cid against the existing models in the collection. Unless the models are all client-side created OR the cid is persisted, there is no way to guarantee that no duplicate models appear.

This becomes a substantial issue when using collections with an API endpoint that returns objects that are uniquely identified using an id. Polling that API endpoint at a set interval, for example, will create many duplicates in the collection.

This pull request adds a property to the options passed to the collection.add method: ```{unique : true}``` that enforces a uniqueness check that relies on both cid and id (as a fallback.) This way calling add will allow for duplicates by default, but it already does for most part.

I will add a jsFiddle and a link to a blog post about this shortly.

tl;dr set unique:true to guarantee no dupe models in your collection.