The current user is the most important one (as I’ve given them a UI to change a field, and after changing it they still see the old values next to the thing confirming they changed them).
Having it work for any user/model would be a bonus. (Having a way for the server to update all clients when anything changes the fields would be the holy grail, but that’s probably too much to hope for.)
Maybe modifying the serializer is the way to go. I will investigate that after some sleep!
The way I’m doing things at the moment:
- I put things into the
user.custom_fields map on the server/Ruby side.
- I whitelisted the fields via the public_user_custom_fields site setting.
- Those are then automatically available on the client/JS side user model in a similar
- I also made an ajax/json API to get and set the fields, which is how the user changes them. But I’ll be displaying the fields in lots of places and from lots of users, so I want to avoid making hundreds of ajax calls to display the data (unless I know it has changed).
As it stands:
- The custom_fields map works great in that it gives me the data on the client side for free, in a convenient map.
- But there are actions the user can do which add or change the data in the map. I use my ajax API to get the server to update its side, and then the user who made the change starts seeing inconsistencies.
- I can’t work out how to update the client-side data (either by modifying the map/object myself, or by forcing the model to reload somehow).
- Main problem is that if the map is empty, it’s null rather than an empty object, and I can’t work out how to create an empty object to add things into. There seems to be something special about it as assigning anything to it crashes my js code.
Perhaps a simple thing I could do is have the client side copy the values out of the map and into normal properties on the user model which are then easier to work with. Bit of a kludge but might work.