Skip to content

Using Backbone Relational to handle JSON-API Data #530

@newtonianb

Description

@newtonianb

We've been using Backbone Relational to model our ORM relationship in the front end for instance:

{
  id: 2
  username: "bob"
  comments: [
     {
        id:5,
        comment: "hi",
        user: {
           username: 'Bob'
        }
     }

  ]
}

That has been working great using models such as this in the front end:

  class User extends App.RelationalModel
    relations: [{
      type: Backbone.HasMany,
      key: 'comments',
      relatedModel: 'Comment',
      collectionType: 'CommentCollection'
    }]

However now our api has changed and respecting more of the JSON-API Spec so the data from the back end is encapsulated inside of 'data'.

{
  data: {
    id: 2
    username: "bob"
    data: {
      comments: [
         {
            id:5,
            comment: "hi",
            user: {
               username: 'Bob'
            }
         }
      ]
    },
    meta: {
    }
  }
}

How can we instruct backbone relational to get the data for the 'comments' relation from .data instead of mapping the json structure directly?

For the ''class User'' we can implement the parse method like so

class User
  parse: (response) ->
    response.data

But how do we do this for the comments relation??

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions