1

I get the error, when I try to save the model with .save()

Converting circular structure to JSON

The funny thing is that modelInstance.toJSON() works just fine.

The error is thrown at backbone.js line 1148 which is:

params.data = JSON.stringify(options.attrs || model.toJSON(options));

Here is how I've setup of the model:

var Clip = Backbone.RelationalModel.extend({
    idAttribute: "mediaItemId",
    defaults: {
        node: {}
    }
});

var clipCollection = Backbone.Collection.extend({
    model: Clip
});

var mainModel = Backbone.RelationalModel.extend({
    url: '/api/v0/videostate',
    relations: [
        {
            type: Backbone.HasMany 
            ,key: 'videoCollection'
            ,relatedModel: Clip
            ,collectionType: clipCollection
            ,includeInJSON: Clip.idAttribute
            ,reverseRelation: {
                key: 'parent',
                includeInJSON: Clip.idAttribute
            }
        }
    ],
});

var modelInstance = new mainModel()

modelInstance.fetch();

The JSON that's loaded into the model:

enter image description here

Tibor Szasz
  • 3,028
  • 2
  • 18
  • 23
  • There must be something in your code that make an object attribute a referrence to itself or one of it's parents. which version of backbone and backbone-relational you are using? – KiT O Oct 20 '13 at 19:10
  • Backbone 1.0 Backbone-relational.js 0.8.6 – Tibor Szasz Oct 20 '13 at 19:36
  • Could you give me return value of `modelInstance.toJSON()` (use pastebin or any other tool like that), and if you are using chrome please see backtrace of error and also tell me last points from backbone-relational. – KiT O Oct 20 '13 at 19:41
  • So, as I can't see and I don't know what `Clip.idAttribute` returns but I don't think that's the `mainModel.idAttribute` (in your reverse relation), change that in your own code! and tell me! – KiT O Oct 20 '13 at 19:55
  • Here is the trace: http://i.imgur.com/fQ2GCxx.png toJSON: http://i.imgur.com/LWQLQsk.png If I change the Clip.idAttribute to "mediaItemId" it works, but it only includes the ID string in the encoded JSON, but I want the full object. – Tibor Szasz Oct 20 '13 at 19:59
  • The problem is exactly in that point. No need to hard-code that, just `includeInJSON: Clip.idAttribute` in reverse relation to `includeInJSON: this.idAttribute`. I'll post answer then. – KiT O Oct 20 '13 at 20:09

2 Answers2

1

Change includeInJSON: Clip.idAttribute in reverse relation to includeInJSON: Clip.prototype.idAttribute

Something like this

{
    type: Backbone.HasMany 
    ,key: 'videoCollection'
    ,relatedModel: Clip
    ,collectionType: clipCollection
    ,includeInJSON: Clip.prototype.idAttribute
    ,reverseRelation: {
       key: 'parent',
       includeInJSON: Clip.prototype.idAttribute
    }
}
KiT O
  • 867
  • 6
  • 21
  • This gives an immediate error of "Cannot read property 'idAttribute' of undefined" – Tibor Szasz Oct 20 '13 at 20:26
  • But it was a stupid idea from me, because Clip is just a function at the time so it can't have a property. – Tibor Szasz Oct 20 '13 at 20:29
  • It works, but the serialized array is now: {"videoCollection":["v1.cdn:91852085.mp4","v1.cdn:94093890.mp4","v1.cdn:94093891.mp4"]... not the whole video model. Maybe I misunderstand what backbone.realated can do. – Tibor Szasz Oct 20 '13 at 20:56
  • If you think the answer is correct then accept (or upvote) it :). That problem can be solved by setting `includeInJSON: ["string array of attributes you want to include"]` [Relational Docs](http://backbonerelational.org/#relations-includeInJSON) – KiT O Oct 20 '13 at 21:19
  • Ohhh, damn. And it's there indeed. Thank you! If you modify your answer from "this.idAttribute" to Clip.prototype.idAttribute I'll accept, because it throws an immediate error. – Tibor Szasz Oct 20 '13 at 21:25
  • Ok, I've updated the answer but that's not correct in general! the reverse relation must have id of it's own class not parent class! maybe there's problem with your api! – KiT O Oct 20 '13 at 21:28
0

Created a JSFiddle with above code , http://jsfiddle.net/ravikumaranantha/PuLxQ/6/, it doesn't throw any error.

var Clip = Backbone.RelationalModel.extend({
    idAttribute: "mediaItemId",
    defaults: {
        node: {} //could be problem here
    }
});

I just sense problem could be (not sure) with having an object in defaults map, you should avoid using objects/arrays in defaults, they will get shared across all instances. If you can post response from fetch call, that should help us debug it further.

Ravi Hamsa
  • 4,721
  • 3
  • 14
  • 14
  • I removed it and still throws error when saving the model. The response to the fetch call is on the screenshot at the bottom of the post. – Tibor Szasz Oct 20 '13 at 18:52
  • @RaviHamsa how you tested with that fiddle? You haven't loaded relational! It just says `TypeError: Backbone.RelationalModel is undefined` – KiT O Oct 20 '13 at 19:18
  • I didn't use the fiddle to test. I tested on my local setup. The fiddle version can't load my local JSON. – Tibor Szasz Oct 20 '13 at 19:20