documentcloud/backbone — pull request #762 by saimonmoore (2011-11-27 13:11:52+00:00)
Title: Is duplicate function

In order to make it less painful for end users to define what makes a model unique within a backbone collection I propose these two changes.

The first commit introduces the following changes:

* In _add(), extract the code checking for an existing model into _getDuplicate().

This funciton will then first check the presence of getByUniqueId() and if not defined will fallback to the existing check with getByCid().

The use of getByUniqueId() allows end users to override the check for duplicity currently used if it doesn't suit their use case.

* The 2nd commit, is a result of the changes introduced by the first commit and is primarily a refactoring to extract the code that maintains the internal indices so that anyone defining getByUniqueId can additionally redefine the index functions to maintain their own indices.

I'd appreciate any feedback....