0

I want to convert a object mixed with Backbone.Events into Json, but if I use JSON.stringify(), I get a error : "TypeError: cyclic object value".

My code :

var object_event = _.extend({
                       attr1: 'value1',
                       attr2: 'value2',
}, Backbone.Events);

var view = new Backbone.View();

view.listenTo(object_event, 'anEvent', function () {
    JSON.stringify(object_event);
});

object_event.trigger('anEvent');
//the error occured

I saw in here : Serializing object that contains cyclic object value, that using the second parameter of JSON.stringify like it is describe is not useable on event objects.

Community
  • 1
  • 1
  • 1
    Why? If it’s for logging purposes, just use `console.log` and don’t bother stringifying, for example. – Ry- Jul 29 '14 at 18:20
  • It's to fetch a collection passing the json as a data – Niconix Jul 29 '14 at 19:05
  • Put the data you need into a new object literal, then. `JSON.stringify({ attr1: object_event.attr1, … })` – Ry- Jul 29 '14 at 19:07
  • It's working yes, thanks. I also found a few minutes ago this solution : JSON.stringify(object_event, [attr1, attr2]) – Niconix Jul 29 '14 at 19:15

0 Answers0