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 9125315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:46:35+00:00 2026-06-17T06:46:35+00:00

In backbone what is the desired way to store the relationship (in localstorage) between

  • 0

In backbone what is the desired way to store the relationship (in localstorage) between an author and books.

Is it possible to have a collection as an attribute or should an array be used or…?

var book = Backbone.Model.extend({ 
    defaults:{ 
        title: ''
    }
});

var books = Backbone.Collection.extend({
    model: book
});


var author = Backbone.Model.extend({
        defaults:{ 
        firstName: '',
        lastName: '', 
        books: new books()
        }
});

var authors = Backbone.Collection.extend({
    model: author,
    localStorage: new Backbone.LocalStorage("authors")
});
  • 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-17T06:46:36+00:00Added an answer on June 17, 2026 at 6:46 am

    There is no One True Way of handling nested collections in Backbone. Quoting from Backbone FAQ:

    Backbone doesn’t include direct support for nested models and collections or “has many” associations because there are a number of good patterns for modeling structured data on the client side, and Backbone should provide the foundation for implementing any of them.

    The FAQ also suggests one pattern and provides a link to the Extensions, Plugins, Resources page, which links to many libraries you can use for handling nested models and model relations.

    That said, I have sometimes approached the problem like this:

    var Author = Backbone.Model.extend({
    
      initialize: function(attrs, options) {
        //convert the child array into a collection, even if parse was not called
        var books = attrs ? attrs.books : [];
        if(!(books instanceof Books)) {
            this.set('books', new Books(books), {silent:true});
        }
      },
    
      //the books property does not need to be declared in the defaults
      //because the initialize method will create it if not passed
      defaults: {
        firstName: '',
        lastName: ''    
      },
    
      //override parse so server-sent JSON is automatically
      //parsed from books array to Books collection
      parse: function(attrs) {
        attrs.books = new Books(attrs.books);
        return attrs;
      },
    
      //override toJSON so the Books collection is automatically
      //converted to an array 
      toJSON: function() {
        var json = Backbone.Model.prototype.toJSON.call(this);
        if(json.books instanceof Books) {
          json.books = json.books.toJSON();
        }
        return json;
      }
    });
    

    The comments hopefully explain how it works, but the point is that you can use the model as you would normally: initialize the children with a collection, an array or nothing, get the from a server, send them to the server, it should all work transparently. There’s quite a bit of boilerplate code to write, but it’s relatively simple to abstract into a base class, if you find yourself repeating the same code.

    Edit: Small correction, you don’t need the books defined in the defaults object, because the constructor creates it if missing.

    /code sample not tested

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

Sidebar

Related Questions

The desired functionality I'm using a Backbone.Collection to view data in a sortable list.
In Backbone, I have a collection which is populated with some JSON data which
Backbone.js documentation suggest loading bootstrapped models this way: <script> var Accounts = new Backbone.Collection;
A Backbone app which I'm developing has a collection and a model, and associated
I am currently learning backbone.js via a screencast tutorial, somewhere along the way, my
I have the following backbone.js controller: App.Controllers.PlanMembers = Backbone.Controller.extend({ routes: { message/:messageType: sendMessage, :
I have a model : window.LayerModel = Backbone.Model.extend({ defaults:{ 'id':'unknow', 'type':'unkonw', 'preId':'unknow', 'data': {}
My backbone.js model has an array property. I bound the change event to save()
In backbone.js, I want to do: initialize: => @collection.on(add, @appendModel(model)) # <- how to
In backbone it seems that I have to get model attributes via model.get('att_name') I'd

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.