backbone-nested
backbone-nested copied to clipboard
Infinite recursion
I had a bug in my code where, by accident, a reference to the window
object was added as an attribute to a NestedModel. That caused an infinite recursion in _setAttr
(and subsequent crash) because window
has a few self-referencing properties like window.window
, window.self
, window._top
, window.parent
, etc.
Even though that was my fault, it revealed that (I think) this could happen with legitimately created recursions (like order.orderLines[0].order
or pet.owner.pets[0]
, etc.)
Maybe NestedModel could try to keep track of the walked objects and avoid the infinite loop.
Hmm, I wonder why this wasn't fixed by #80... mind submitting a failing test (that preferably doesn't involve window
)?
I should have mentioned the version I'm running: 1.1.2 from July 16th, which seems to include the fixes from #80. Maybe it only fails for window
then.
OK, I don't have a test but pasting this in a JS console seems to recreate the error:
var father = {name: 'bob'}
var child = {name: 'danny', father: father}
father.children = [child]
var family = new Backbone.NestedModel({lastName: 'Smith', head: father})
family.set({'head.name': 'Bobby'})
family.set({'head.name': 'Robert'})
family.set({'head.name': 'Rob'})
// Now try the same value again:
family.set({'head.name': 'Rob'})
// ==> RangeError: Maximum call stack size exceeded
Any updates on this going to happen soon?
@vkovalskiy unfortunately, both I and @afeld are pretty busy. I'll try to get to this in the following week or two.
@gkatsev thanks for reply. However, I believe that the solution would be to simply forbid recursive models because you'll never know when it's ok to stop traversing. I believe the best that can be done here is informing the user that model has recursive attrbutes and throw
, possibly stating the problem attribute name.
At least, there would be no browser doom state J
I agree with @vkovalskiy. Throwing with a nice error message is better than trying to guess the best way to handle it.
That may very well be the best way to handle it :grin: