You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
Components can be provided a key, via the special key parameter that ensures when rendering lists of components the ordering does not change, and speeds up the rendering of the list.
typically you can use some data associated with the values being displayed (like record.id above) or the ruby object_id, or some other unique identifier depending on the contents of the list item.
For HyperModels (and probably other places too) this can be difficult. The above example for instance only works if all the records are saved (i.e. have ids). You can't use record.object_id since there is no guarantee that on the next rendering the record instance will be the same object_id, so you have use record.backing_record.object_id which is gross and relies on knowing the inards of HyperModel.
The solution is to add a to_key method to the base ruby Object class and to have the key parameter automatically have to_key applied to it. The default to_key method will return self.object_id, but this can be overwritten as needed. I.e. ActiveRecord::Base can override as self.backing_record.object_id.
Now we can write DisplayRecord(record: record, key: record) shorter, sweeter, and works.
Also in keeping with POLS, strings, symbols, booleans and number objects will return themselves.
This is nice. The way it's laid out is also backwards compatible because record.id or record.object_id in existing code returns a number, and Number#to_key == self.
Activity
sfcgeorge commentedon Feb 9, 2018
This is nice. The way it's laid out is also backwards compatible because
record.idorrecord.object_idin existing code returns a number, andNumber#to_key == self.Donnadieu commentedon Feb 17, 2018
Hi, I think I can help with this issue.