# Reload currentUser object

**URL:** <https://meta.discourse.org/t/reload-currentuser-object/39876>\
**Category:** Development\
**Created:** [February 19, 2016, 9:16pm UTC](https://meta.discourse.org/t/reload-currentuser-object/39876 "2016-02-19T21:16:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)\
**Post date:** [February 19, 2016, 9:16pm UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/1 "2016-02-19T21:16:47Z")

</div>

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:

[https://github.com/joebuhlig/discourse-feature-voting](https://github.com/joebuhlig/discourse-feature-voting)

---

<div class="post-metadata">

**Author:** ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)\
**Post date:** [February 20, 2016, 1:03am UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/2 "2016-02-20T01:03:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)\
**Post date:** [February 20, 2016, 1:44am UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/3 "2016-02-20T01:44:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [February 20, 2016, 1:44am UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/4 "2016-02-20T01:44:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)\
**Post date:** [February 20, 2016, 1:46am UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/5 "2016-02-20T01:46:27Z")

</div>

I think we posted at the same time, Sam. 😉

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?

---

<div class="post-metadata">

**Author:** ![LeoDavidson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leodavidson/32/119574_2.png) [@LeoDavidson](https://meta.discourse.org/u/LeoDavidson)\
**Post date:** [February 25, 2017, 8:15am UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/6 "2017-02-25T08:15:20Z")

</div>

> [@sam](#):
>
> I would change is so Ajax call returns all the info needed to update current user and then update it with that info

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.

---

<div class="post-metadata">

**Author:** ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)\
**Post date:** [February 25, 2017, 12:19pm UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/7 "2017-02-25T12:19:01Z")

</div>

Is this for the current user?

For the voting plugin I had to add [methods to the model](https://github.com/discourse/discourse-feature-voting/blob/master/plugin.rb#L117) for each custom field I wanted to display client side. That allowed me to [add the custom fields to the current user serializer](https://github.com/discourse/discourse-feature-voting/blob/master/plugin.rb#L182) 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:

[https://github.com/discourse/discourse-feature-voting/blob/master/assets/javascripts/discourse/widgets/vote-box.js.es6#L33](https://github.com/discourse/discourse-feature-voting/blob/master/assets/javascripts/discourse/widgets/vote-box.js.es6#L33)

---

<div class="post-metadata">

**Author:** ![LeoDavidson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leodavidson/32/119574_2.png) [@LeoDavidson](https://meta.discourse.org/u/LeoDavidson)\
**Post date:** [February 25, 2017, 1:32pm UTC](https://meta.discourse.org/t/reload-currentuser-object/39876/8 "2017-02-25T13:32:30Z")

</div>

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.
