Hi!
I ran into an issue (offirgolan/ember-cp-validations#507) while introducing ember-cp-validations into a project where creating Ember.Objects with an ownerInjection results in TypeError: Converting circular structure to JSON. I have provided an Ember Twiddle https://ember-twiddle.com/979d8fc762574b457e04393cd9852159?openFiles=controllers.index.js%2C that demonstrates this behavior.
As the default Ember Data ajaxOptions function calls JSON.stringify, I overrode this function in my app to ignore the __OWNER__ property.
I don't know that addressing the circular reference makes sense in this context as the entire instance would then appear in the stringified representation so perhaps Ember Data is the correct repo to report this in? If that is the case, please let me know and I'll close this in favor of an issue in the Ember Data repo. Thanks :)
I included a link to an Ember Twiddle that reproduces this:
https://ember-twiddle.com/979d8fc762574b457e04393cd9852159?openFiles=routes.index.js%2C
Ember Version 2.12.0 per twiddle.json:
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
@rwjblue I thought we moved owner stuff to meta? Or is that WIP
We should implement a toJSON on Ember.ApplicationInstance, that emits useful information but avoids the circular reference.
For what its worth, I recently upgraded from ember 2.12 to ember 2.14 and the __OWNER__ property appears to have been replaced by __OVERRIDE_OWNER__
In case this helps anyone in a similar situation... Our product has a custom datastore with models that also need to have owner set but also be JSON.stringified. We solved/hacked this by defining the owner ourselves instead of using Ember.setOwner(obj, val) directly.
setOwner just sets obj[OWNER] = val but the tricky bit is knowing what theOWNER constant is, since it changes all the time and is not easily accessible.
function getOwnerKey() {
const x = {};
Ember.setOwner(x);
return Object.keys(x)[0];
}
const model = Ember.Object.create(data);
Object.defineProperty(model, getOwnerKey(), {
value: Ember.getOwner(this),
enumerable: false
});
@rwjblue: if you could describe what you would consider "useful information" I would be glad to give this a try.
@rwjblue @rynam0 @stefanpenner @vincent99 is this still an issue, perhaps we should close or create a new reproduction of this, what do you think?
I mean we just use the hackaround as I described above in our product, but it is still an issue.. All you really need to reproduce it is:
var cycle = Ember.Object.create();
cycle.set('cycle', cycle);
var x = Ember.Object.create();
Ember.setOwner(x, cycle);
try {
JSON.stringify(x);
} catch (e) {
alert(e);
}
Here's a twiddle of the same: https://ember-twiddle.com/b11d1084021dd0ff1dcf19226f2d7d01
And the fix would be to have setOwner() define it as a non-enumerable property as I did above.
Thanks everyone for the discussion. Closing as not relevant in a post-Octane world.
Confirmed. I was able to delete my overridden ajaxOptions function. Thanks!
Most helpful comment
I mean we just use the hackaround as I described above in our product, but it is still an issue.. All you really need to reproduce it is:
Here's a twiddle of the same: https://ember-twiddle.com/b11d1084021dd0ff1dcf19226f2d7d01
And the fix would be to have
setOwner()define it as a non-enumerable property as I did above.