Sorry if this is a bit convoluted... I am still learning Backbone.js...
What is the proper way to load & save Backbone models that have sub-models within themselves? (And should I even be having sub-models?)
For example, (pardon the coffeescript), if I have something like:
class Address extends Backbone.Model
urlRoot: '/api/v1/address/'
url: -> return @urlRoot+@id+'/?format=json'
defaults: {'city': '', 'state': ''}
class Person extends Backbone.Model
urlRoot: '/api/v1/person/'
url: -> return @urlRoot+@id+'/?format=json'
defaults: { name: 'Anon', address: new Address }
... and then I do this ...
dude = new Person
dude.set('id',101)
dude.fetch()
// Response returns {name: 'The Dude', address: '/api/v1/address/1998/'}
// And now, dude.get('address') is '/api/v1/address/1998' and no Address object
where = new Address
where.set('id',1998)
where.fetch()
// Response returns {city: 'Venice', state; 'CA'}
What I want is to say dude.fetch() and for it to get both the dude and his address, and when I call Backbone.sync('update',dude), I want to save both the dude and his address. How?
On the backend, I am using tastypie to construct my api for some SQLAlchemy tables (not Django's ORM), and so I have a resource for my Person table and Address table:
class AddressResource(SQLAlchemyResource):
class Meta:
resource_name = 'address'
object_class = AddressSQLAlchemyORMClass
class PersonResource(SQLAlchemyResource):
address = ForeignKey(AddressResource, 'address')
class Meta:
resource_name = 'person'
object_class = PersonSQLAlchemyORMClass
Tastypie's ForeignKey function creates a mapping that returns the URL to the address in question.
I tried overloading the Dude.parse() function to call fetch for the Address(), but it wasn't working and it felt wrong, and it raised all sorts of questions:
- Should I be modifying my tastypie response to include the Address as a nested object?
- If I change to a nested object, should I be use backbone-relational, as in the question Backbone-Relational related models not being created, or is that overkill?
- Should I be overloading the parse() or fetch() function or creating my own backbone.Sync() to get the response and then do this manually?
- Since it is one-to-one, should I instead just have one model, instead of a sub-model, and send the information back forth together in one request?
Is there a standard way of doing this?