PaulUithol/Backbone-relational — Issue #62: How to handle the case when a reference is broken
Author: sebpiq
Created: 2012-01-10 12:36:32+00:00
State: open

Hi !

I have a Message model, which has two HasOne relations to a User model.

```
Message = Backbone.RelationalModel.extend({
urlRoot: 'views/Message/',
defaults: {
text: undefined,
recipient: undefined, // Foreign key to User
sender: undefined, // Foreign key to User
},
relations: [{
type: Backbone.HasOne,
key: 'recipient',
relatedModel: 'User',
collectionType: 'User',
includeInJSON: true
}, {
type: Backbone.HasOne,
key: 'sender',
relatedModel: 'User',
collectionType: 'User',
includeInJSON: true
}],
});
```

The server sends to the frontend some updates with new messages received, and the 'sender' and 'recipients' attributes appear as usernames ... problem is that the frontend might not know this user.
For some complicated backend reasons, we have to deal with that, and the server cannot provide us more info.

Of course - when trying to serialize the message for rendering it, and in a few other cases - when the relation is broken, the page just crashes.

So my question : is there a generic way to handle a broken relation (as we'll face the same problem in other models as well) ? Any hook, any standard place were a default value can be set ?