hasMany relationships fail when a foreign key gets reset to a different record
Reported by Christopher Swasey (endash) | November 10th, 2008 @ 05:16 PM
Say you have two models: Page, and Item. A Page has many Items. If you try to reconfigure the relationships by setting the 'page' on an Item, the collection of Items on the new owner Page gets notified to update itself, but when it does, it collects the items of the last Page object to be instantiated, whichever that happens to be.
The problem is in record.js#hasMany. Keep in mind that #hasMany gets called just once, when the model (as opposed to individual record objects) is created. #hasMany builds an options/conditions object for the collections, and then returns a function which does the actual creation/lookup of the collection.
The problem is that the scope of the options object is #hasMany, and not the returned function. That means that the same options and conditions objects are used for every single record of that type, the conditions object gets changed as each record is instantiated, and thus every record's collection 'thinks' that it's the very last record of its type to be instantiated, ergo major foulups when it comes time to reload the association. The associations load correctly initially, because they get refreshed right after that particular record has overwritten the conditions object for everyone else.
The solution is to ensure that the scope of the object getting passed to the collection is the function that gets returned by #hasMany, and that's accomplished simply by moving a few lines of code from outside the function into the function.
Comments and changes to this ticket
-
Christopher Swasey (endash) November 10th, 2008 @ 05:16 PM
- no changes were found...
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
SproutCore: JavaScript Application Framework
Source available from github
The Git repository resides at http://github.com/sproutit
Check out the current development trunk with:
git clone git://github.com/sproutit/sproutcore.git
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Submitting Patches
Resist adding new tags, use the ones in the tag cloud already. If you are submitting a patch or a pull request from a fork, tag the ticket as a "patch". If you are submitting a new feature enhancement, tag it as "enhancement". If you are submitting a fix for a bug, tag it as "fix".
Thanks!
We're glad you're contributing. Feel free to drop by the Google Group as well to let us know you are involved.
