documentcloud/backbone — pull request #1148 by epeli (2012-03-28 10:56:19+00:00)
Title: Use function to create models on collections

instead of just `new this.model(attrs, options)`. Small change, but his makes model creation on extended Collections more flexible because the function is easily overridable.

### Use case

Server returns list of different types of data on `collection.fetch()`.

For example files and directories:

```json
[
{ name: "myfile",
type: "file"} ,

{ name: "mydir"
type: "dir" }
]
```

For that I'd like to create different models for files and directories like this:

```javascript
var FileSystem = Backbone.Collection.extend({
createModel: (attrs, options) {
if (attrs.type === "file") {
return new File(attrs, options);
}
else if (attrs.type === "dir") {
return new Directory(attrs, options);
}
else {
throw new Error("Bad type: " + attrs.type);
}

}
});
```
All tests passes after this change.