Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for circular references in checkChanges #80

Merged
merged 1 commit into from
Jul 15, 2013

Conversation

yepitschunked
Copy link
Contributor

Backbone collections set up a circular reference: collection[i] is a reference
to a model, and model.collection is a reference back to the collection.
checkChanges needs to not run in circles when encountering this.

Fixes #79

@afeld
Copy link
Owner

afeld commented Jul 13, 2013

Tests please! :-)

if(visited[obj]) {
return;
} else {
visited[obj] = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work, because Models (by default) will be serialized to "[object Object]". Might need to be more clever about how to index them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, my brain was stuck in ruby land. Looks like Javascript has no concept of "object id" but it does support comparisons. Might just have to use an array and scan over it repeatedly...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with a less-than-optimal implementation to start... as long as there are tests. Also, because you're modifying the visited object in the function anyway, just define that variable outside of the closure (as opposed to passing it to each call of the method.

Backbone collections set up a circular reference: collection[i] is a reference
to a model, and model.collection is a reference back to the collection.
checkChanges needs to not run in circles when encountering this.
@yepitschunked
Copy link
Contributor Author

Revised to actually work, and added tests. Without the change, the test fails with a "Maximum call stack size exceeded" error.

afeld added a commit that referenced this pull request Jul 15, 2013
Check for circular references in checkChanges
@afeld afeld merged commit 3a5a331 into afeld:master Jul 15, 2013
@afeld afeld mentioned this pull request Sep 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop when assigning a Backbone.collection
2 participants