Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I load sub-models with a foreign key relationship in Backbone.js?
    primarykey
    data
    text
    <p>Sorry if this is a bit convoluted... I am still learning Backbone.js...</p> <p>What is the proper way to load &amp; save Backbone models that have sub-models within themselves? (And should I even be having sub-models?)</p> <p>For example, (pardon the coffeescript), if I have something like:</p> <pre><code>class Address extends Backbone.Model urlRoot: '/api/v1/address/' url: -&gt; return @urlRoot+@id+'/?format=json' defaults: {'city': '', 'state': ''} class Person extends Backbone.Model urlRoot: '/api/v1/person/' url: -&gt; 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'} </code></pre> <p><strong>What I want</strong> 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?</p> <p>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:</p> <pre><code>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 </code></pre> <p>Tastypie's <a href="http://django-tastypie.readthedocs.org/en/latest/interacting.html" rel="nofollow noreferrer">ForeignKey</a> function creates a mapping that returns <strong>the URL</strong> to the address in question.</p> <p>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:</p> <ol> <li>Should I be modifying my tastypie response to include the Address as a nested object?</li> <li>If I change to a nested object, should I be use <a href="https://github.com/PaulUithol/Backbone-relational/" rel="nofollow noreferrer">backbone-relational</a>, as in the question <a href="https://stackoverflow.com/questions/11316782/backbone-relational-related-models-not-being-created">Backbone-Relational related models not being created</a>, or is that overkill?</li> <li>Should I be <a href="http://paltman.com/2012/04/30/integration-backbonejs-tastypie/" rel="nofollow noreferrer">overloading the parse()</a> or fetch() function or creating my own backbone.Sync() to get the response and then do this manually?</li> <li>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?</li> </ol> <p>Is there a standard way of doing this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload