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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:05:30+00:00 2026-05-31T19:05:30+00:00

Been trying to get this up and running for a while now. Basically i

  • 0

Been trying to get this up and running for a while now.
Basically i have a rest api as backend that returns json, all fine.

The problem i have is with backbone-relational. I’m sure i’ve just got something
wrong but i’ve been searching the nets for a while and couldn’t find anything to help.

I’m trying to get a has many relationship between the model “Profession” and “Weapon”.
Here’s my code for that:

Profession = Backbone.RelationalModel.extend({
    urlRoot: '../api/professions',

    relations: [{
        type: Backbone.HasMany,
        key: 'weapons',
        relatedModel: 'Weapon',
        collectionType: 'Weapons',
        reverseRelation: {
            key: 'profession',
            includeInJSON: 'id',
            keySource: 'profession_id'
        }
    }]
});

Weapon = Backbone.RelationalModel.extend({
    urlRoot: '../api/weapons'
});
Weapons = Backbone.Collection.extend({
    model: Weapon,

    url: function(models){
        return '../api/weapons';
    }
});

And a call to those api endpoints returns:

{name: "Profession1", id: 1}

[{name: "Weapon1", profession_id: 1}, {name: "Weapon2", profession_id: 1}]

So, if i understand correctly a profession.fetchRelated('weapons') should send an httprequest to the url for the weapons collection and pull the object which have a profession_id of 1. But nothing happens when i run profession.fetchRelated('weapons')

  • 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-31T19:05:30+00:00Added an answer on May 31, 2026 at 7:05 pm

    So, if i understand correctly a profession.fetchRelated(‘weapons’) should send an httprequest to the url for the weapons collection and pull the object which have a profession_id of 1. But nothing happens when i run profession.fetchRelated(‘weapons’)

    This is not how I understand Backbone-relational to work – with the caveat that I’m working with an older version, and have not yet investigated the latest versions.

    From my understanding, Backbone-relational wants data that looks different from your API responses.

    I think that Backbone-relational wants references to nested models in the parent model response, either fully nested:

    {
      name: "Profession1", 
      id: 1, 
      weapons: [
        {name: "Weapon1", profession_id: 1}, 
        {name: "Weapon2", profession_id: 1}
      ]
    }
    

    In which case, it will fully populate the nested models.

    Or, as references:

    {
      name: "Profession1", 
      id: 1, 
      weapons: ['weapon1','weapon2']
    }
    

    In which case Backbone-relational will stash the related keys internally, but present an empty collection. You would then use fetchRelated to fetch the full data for the related models:

    assassin.fetchRelated('weapons')
    

    Which make a request for weapons with id 'weapon1' and 'weapon2', either as individual requests, or if your API accepts a set request, a single request, expecting a response something like:

    {id: 'weapon1', name: "Weapon1", profession_id: 1}
    

    I don’t, off the top of my head, know if there’s a quick, built-in way to work with your API responses, other then to write a custom function.

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

Sidebar

Related Questions

I have been trying to get around this error for a day now and
I have been trying to days now to get this website completed ... however,
I have been trying to get this one section of my UI to immediatly
This is crazy, I have been trying for hours to get this to work.
I have been struggling with this for a while now so I thought I
I've been trying to figure this out for a while now and can't figure
I have been trying this for a week or so now. I have followed
I've been trying to get this code to work for hours! All I need
I've been trying to get this module to work and no matter what I've
I've been trying to get this Sudoku game working, and I am still failing

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.