documentcloud/backbone — pull request #2144 by shiloa (2013-01-16 09:35:30+00:00)
Title: Adding a 'fallback' argument to Model#get

Motivation

Sometimes it's useful to retrieve some default value from a Model, even if that value doesn't exist in the db (or not even an actual model attribute, if waiting implementation).

This fallback value isn't persisted, but rather acts as a sane default when the actual value returns true on `_.isEmpty( value )`. I found it useful in view templates, when I don't want to inline a lot of ternary logic like:

```erb
<%= @user.has('attribute') ? @user.get('attribute') : 'N/A' %>

<%= @user.get('attribute', 'N/A') %>
```

It is basically equivalent to Python's `dict.get(key, default=)` or Ruby's `Hash#fetch(key, [default])`. I do realize `Model#get` is a core method, so I'm not taking this addition lightly.

I've searched existing issues, but couldn't find something similar. Apologies in advance if I missed one. I did my best to stick to the guidelines, so hopefully things are in order.

Thanks!