Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:15:33+00:00 2026-06-08T00:15:33+00:00

Sorry if this is a bit convoluted… I am still learning Backbone.js… What is

  • 0

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:

  1. Should I be modifying my tastypie response to include the Address as a nested object?
  2. 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?
  3. Should I be overloading the parse() or fetch() function or creating my own backbone.Sync() to get the response and then do this manually?
  4. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:15:35+00:00Added an answer on June 8, 2026 at 12:15 am

    With Tastypie, you can change the response to being a nested object, instead of a link, by specifying full=True in the ForeignKey definition:

    class PersonResource(SQLAlchemyResource):
        address = ForeignKey(AddressResource, 'address', full=True)
    

    This returns the address object along with the person.

    Next, I still don’t know if this is the best way, but I moved my sub-model out of the attributes, and overloaded parse() to set it, and update() to save it, like:

    class Person extends Backbone.Model
      address: new Address
      urlRoot: '/api/v1/person/'
      url: -> return @urlRoot+@id+'/?format=json'
      defaults: { name: 'Anon'}
    
      parse: (response) ->
         addressResp = response.address || {}
         addressAttr = @address.parse(addressResp)
         @address.set(addressAttr)
         return response
    
      update: (options) ->
         @set('address',@address.toJSON())
         Backbone.sync 'update', @, options
    

    In this example, I could add the address back to the attributes and manage it with set/get, but then in my own environment, I’ve created an alternate toJSON function for the json to post to the server, and couldn’t find a good way to set the json to the object without changing it from a collection to a json response.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a bit of a noob question but I am still
Sorry of this appears a bit simple a question, but is there a way
Sorry if this is a bit random, but is it good practice to give
I'm sorry if this is a bit of a C-noob question: I know I
Sorry if this question is a bit open ended, but I'm pretty new to
Sorry if this question seems a bit complex but I think its all related
I'm sorry if this question is a bit basic, or if I've missed an
I've done quite a bit of searching for this, so I'm sorry if this
Sorry. I'm just bitten (or bit. Go figure with this left-to-right language :-) I've
I know this has a stupid solution but, sorry, I'm little bit confused. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.