backbone.js - Using Backbone Relational to handle JSON-API Data -
we've been using backbone relational model our orm relationship in front end instance:
{ id: 2 username: "bob" comments: [ { id:5, comment: "hi", user: { username: 'bob' } } ] }
that has been working great using models such in front end:
class user extends app.relationalmodel relations: [{ type: backbone.hasmany, key: 'comments', relatedmodel: 'comment', collectiontype: 'commentcollection' }]
however our api has changed , respecting more of json-api spec data end encapsulated inside of 'data'.
{ data: { id: 2 username: "bob" data: { comments: [ { id:5, comment: "hi", user: { username: 'bob' } } ] }, meta: { } } }
how can instruct backbone relational data 'comments' relation .data instead of mapping json structure directly?
for ''class user'' can implement parse method so
class user parse: (response) -> response.data
but how comments relation??
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; }
Comments
Post a Comment