2

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??

user391986
  • 29,536
  • 39
  • 126
  • 205

1 Answers1

0

How's this?

parse: function (response) {
    var fixed_response = response.data;
    fixed_response.comments = fixed_response.data.comments;
    delete fixed_response.json.data;
    return fixed_response;
}
David Fusilier
  • 301
  • 2
  • 8