Reload currentUser object

I’m in the midst of working on a plugin that has me changing the values in a custom_field that are used to calculate a few user methods. I’m then passing those methods to the current user serializer.

In the process I have an ajax call that updates one of these user custom_fields. But that doesn’t update the currentUser object on the front-end. I’m still a bit new to Ember but is there a way to reload that currentUser object?

This may be a bit difficult to follow. Here’s the source code I’m currently working with:

1 Like

You can add a callback on the Ajax call to update the state of the object client side. :smile:

Makes sense. I get the callback process. And I can do a reload of the topic model. But how do you do that on the current user object? Or are you talking editing the object property?

I would change is so Ajax call returns all the info needed to update current user and then update it with that info

1 Like

I think we posted at the same time, Sam. :wink:

That makes sense, I think. So I can calculate the new property value server side and then edit the object client side to match the server side?


Can anyone help with how to do this?

In particular, if user is a user model on the JS client side, and there were no custom fields for the user in question when their model was loaded, then user.custom_fields is null and I can’t work out how to turn it into the right type of object to then be able to add the new values to it.

Is this for the current user?

For the voting plugin I had to add methods to the model for each custom field I wanted to display client side. That allowed me to add the custom fields to the current user serializer which made it possible to use them client-side.

From there, you can update the user object on the client after the ajax call has completed:


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 user.custom_fields map.
  • 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.

1 Like