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

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top