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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:53:37+00:00 2026-05-26T12:53:37+00:00

I’m having a heckuva time understanding the documentation for Backbone-Relational ; it’s not 100%

  • 0

I’m having a heckuva time understanding the documentation for Backbone-Relational; it’s not 100% clear on which relation to add things like includeInJSON. Probably best to describe my confusion by illustrating the structure I’m trying to create. I have a Venue model that has zero or more nested Address models (1:n relationship). The backend store is MongoDB, which can have embedded objects. I’d like to store it in this format:

{
    id: 12345,
    label: 'OPUS Cafe Bistro',
    addresses: [
        {
            type: 'mailing',
            address1: '#52 - 650 Duncan Ave',
            city: 'Penticton, BC'
        },
        {
            type: 'main',
            address1: '#106 - 1475 Fairview Rd',
            city: 'Penticton, BC'
        }
    ]
}

(Please ignore the ugly data structures; I’ve adjusted it for brevity.) Now I believe I set up the relationship between Venue and Address thusly:

var Venue = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasMany,
            key: 'addresses',
            relatedModel: 'Address',
            includeInJSON: false,
            collectionType: 'Addresses',
            reverseRelation: {
                key: 'venue'
            }
        }
});

If I understand correctly, I set includeInJSON to false in order to prevent the Venue from being serialised into the venue key in Address, but under reverseRelation I leave includeInJSON blank in order to have the full Address (without a venue property) serialised as an array in the addresses property of the Venue – as per my hoped-for example. Correct?

By the same token, I’m trying to understand the same concept in relation to a join-style relationship. Consider that Venue now has an organisationID field:

/* venue in JSON format  */
{
    id: 12345,
    organisationID: 336,
    label: 'OPUS Cafe Bistro',
    addresses: []
}

/* and now for the organisation */
{
    id: 336,
    label: 'OPUS Entertainment Group'
}

Using the examples in the documentation, which seem to prefer the Backbone.HasMany relationship, I think that I’d have to set up Organisation thus:

var Organisation = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone:HasMany,
            key: 'venues',
            relatedModel: 'Venue',
            includeInJSON: Backbone.Model.prototype.idAttribute,
            collectionType: 'Venues',
            reverseRelation: {
                key: 'organisationID',
                includeInJSON: false
            }
        }
    ]
});

… which should serialise into the above example, right? (I.e., Venue grabs Organisation‘s id and inserts it into organisationID, and Organisation doesn’t serialise any list of Venues)

Thanks in advance for any help – looking forward to using this handy library, after clawing my eyeballs out trying to write my own relational glue for Backbone.js 🙂

  • 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-26T12:53:38+00:00Added an answer on May 26, 2026 at 12:53 pm

    (copying my answer from https://github.com/PaulUithol/Backbone-relational/issues/37 over here, so more people can hopefully find it)

    I’m sure you’ve figured it out by now, sorry for replying so late, but just to be sure: options (like collectionType and includeInJSON) apply to the key that’s within the same object.

    So, for your first example, you could write the relation as:

    var Venue = Backbone.RelationalModel.extend({
        relations: [
            {
                type: Backbone.HasMany,
                key: 'addresses',
                relatedModel: 'Address',
                includeInJSON: true,
                collectionType: 'Addresses',
                reverseRelation: {
                    key: 'venue',
                    includeInJSON: 'id'
                }
            }
    });
    

    This will create a Venue that HasMany addresses. The addresses key for any given Venue uses the collectionType Addresses, and is fully serialized because includeInJSON is set to true (not that the default value for includeInJSON is also true; so if you don’t specifiy it, it will fully serialize a relation or reverseRelation).

    The reverseRelation simply takes the same options, only applies them with respect to the venue key on any Address. In this case, it will only serialize the venue.id property for the venue it’s linked to.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.