documentcloud/backbone — pull request #1717 by davidgtonge (2012-10-08 04:59:33+00:00)
Title: Stop Backbone throwing an error when an invalid model is added

Current errors are thrown in the following places in Backbone:

- Syncing without defining a url
- Sorting a collection without a comparator
- Starting Backbone History twice
- Trying to delegate an event to a non existent method
- Adding an invalid model to a collection

The first four errors all relate to a bugs in a users code and should understandably be thrown. The last error however is the result of invalid data being sent to the application. This data may have come from a source outside the users control or there may be a bug in the users server code. Either way I think an error shouldn't be thrown when trying to add an invalid model to a collection.

I was just bitten by this recently and I think this kind of error should trigger an error event rather than throwing an error. I had an application running smoothly for months, until some hidden server-side bug caused an empty model to be sent to the client along with about 100 valid models. The empty model prevented the other models being added. This pull request throws away invalid models and triggers an error event on the collection, rather than throwing an error and stopping the application.

If this pull request is not the answer, I think there still needs to be a better way of handling this.
In making the change I also removed a loop in Collection.add, - so this change should bring a small performance boost.

Similar issues:
https://github.com/documentcloud/backbone/pull/484