Tibor Szasz
Tibor Szasz

Reputation: 3227

Backbone relational model saving error

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

Upvotes: 1

Views: 811

Answers (2)

Ravi Hamsa
Ravi Hamsa

Reputation: 4721

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.

Upvotes: 0

KiT O
KiT O

Reputation: 867

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

Upvotes: 1

Related Questions