I have simple nested JSON:
{
"user": {
"name": "John",
"related": {
"name": "Alice",
"related": {
"name": "Bob"
}
}
}
}
And I have Backbone Relational model “User”:
(function(){
var User = Backbone.RelationalModel.extend({
defaults: {
name: "",
related: {}
},
// Linking this model to itself
relations: [{
type: Backbone.HasOne,
key: "related",
relatedModel: User // As I've expected it doesn't exist at this time
}]
})
})()
So, question is: How to link model to itself? Thank you!
Backbone.Relational “related” models need to be in an accessible scope, and in earlier versions of Backbone-Rel, it generally expected the models to be accessible in the global scope.
But, if you’re using Require.js, or wrapping your model definitions in a closure, they won’t be in that global scope.
This pull request deals with this situation. With the latest HEAD of Backbone-Rel, you can add in a custom scope for Backbone-Rel to look for related models in:
Live jsFiddle example: http://jsfiddle.net/edwardmsmith/BRmQF/16/