Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetchRelated not fetching related

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')

like image 711
JonasFromell Avatar asked Mar 15 '12 06:03

JonasFromell


1 Answers

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.

like image 176
Edward M Smith Avatar answered Sep 30 '22 15:09

Edward M Smith