0
votes

Normally when I use backbone.js I would apply this sort of patch to nest model attributes the way that Rails expects them:

Backbone.Model.prototype.toJSON = function() { 
  var hashWithRoot = {}; 
  hashWithRoot[this.modelName] = this.attributes; 
  return _.clone(hashWithRoot);
};


App.Models.Card = Backbone.Model.extend({
  modelName:  'card'
});

However, in my current project I'm using Backbone Relational, which is great, but it has its own toJSON override. The result is that I get a circular reference exception in my console:

Uncaught TypeError: Converting circular structure to JSON

Has anyone managed to successfully produce nested JSON for PUT/POST to a Rails backend with Backbone Relational?

EDIT 2:

I created a jsfiddle that shows the collections and models in question - and included some json to bootstrap the app. For some reason (maybe because I'm hungry) I can't get the fiddle to work.

Hopefully that gives a better idea of what I'm trying to do. I'm going to eat then try to fix the fiddle...

Cheers, Stu

1

1 Answers

1
votes

Is it uncouth to be writing a non-answer answer? Because I can’t write comments…

You need to give us more information. I haven’t used Backbone Relational, but I made a fiddle that manages to serialise a model with a HasMany relation. There are some extraneous and incorrect attributes, but no circular structure error.

What relationships are you using? Can you make a fiddle that is more similar to your application’s structure?