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

  • SEARCH
  • Home
  • 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 7415167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:14:53+00:00 2026-05-29T07:14:53+00:00

I am working on a project where we have data models that look basically

  • 0

I am working on a project where we have data models that look basically like this…

var library = {
    location: 'Somewhere'
    books: [
    {
        name: 'Good Book',
        publisher: 'Great publisher'
        authors: [
        {
            name: 'Joe Schmoe',
            age: '65'
        },
        {
            name: 'Robert Smith',
            age: '47'
        }
    }  
}

I am trying to figure out what the best way of laying out the model structure.

Something like…

var LibraryModel = Backbone.Model.extend({});

var Book = Backbone.Model.extend({});
var Books = Backbone.Collection.extend({
    model: Book
});

var Author = Backbone.Model.extend({});
var Authors = Backbone.Collection.extend({
    model: Author
});

Is this the best way to tackle this? Has anyone else faced anything like 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-05-29T07:14:54+00:00Added an answer on May 29, 2026 at 7:14 am

    Whether or not you need N to N, 1 to N or whatever relationships is largely a back-end concern, IMO. Unless you dealing strictly with CRUD screens, Backbone models typically don’t translate directly to a back-end model, though they often approximate a back-end model.

    That being said, Backbone-relational is definitely a good option for handling situations like this. It provides a very robust set of features for handling relational code and is worth looking in to.

    If you’d rather stay clear of a large plugin and it’s associated configuration and requirements, though, there are some simple tricks that you can use in your models directly. Since you know your data structure up front, you can make large assumptions about the code you need to write instead of having to create a very flexible system like BB-Relational.

    For example, getting a list of book into a library, using the above data structure, can be as simple as:

    
    Book = Backbone.Model.extend({});
    Books = Backbone.Collection.extend({
      model: Book
    });
    
    Library = Backbone.Model.extend({
      initialize: function(){
        this.parseBooks();
      },
    
      parseBooks: function(){
        var data = this.get("books");
        this.unset("books", {silent: true});
        this.books = new Books(data);
      }
    });
    

    I’ve used this code half a dozen times and it’s worked out just fine for me every time. You can extend this same pattern and set of assumptions in to each layer of your object model.

    You might also need to override the toJSON method on your model:

    
    Library = Backbone.Model.extend({
      // ... code from above, here
    
      toJSON: function(){
        var json = Backbone.Model.prototype.toJSON.call(this);
        json.books = this.books.toJSON();
        return json;
      }
    });
    

    Depending on how far down this path you need to go, you will likely end up re-creating half of the functionality that BB-Relational already provides. It might be easier to use a plugin like that, with declarative relationship handling if you have a large enough structure.

    IMO, it’s worth understanding these basic patterns, knowing how to do this for yourself before you dig in to BB-Relational. But if you’re constrained for time, I’d start with BB-Relational or some other relational mapper (I think there are a few more out there, these days).

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

Sidebar

Related Questions

I'm actually working at MOSS 2007 project where I have to import data from
I'm working on a project in which we have a database, data layer (entity
I have project that I'm working on that is going to require a webserver.
I have a fully working Setup project within Visual Studio 2008 that takes inputs
I'm working on a project where I have 2 web services that need the
I'm working on a project that performs physiological simulations using mathematical models. We currently
I'm working on an MVC 2 project and I have a model that looks
I have an MVC3 project I'm working on that has a View with an
I working on project and have problem with threading and update of UI. I
I’m working on a project and have developed the high level user requirements for

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.