Maintaining a javascript “mirror” of your user object?

Have a look at the js-data-helper plugin which gives you rails helpers that dump data attributes into DOM elements.

Up vote 0 down vote favorite share g+ share fb share tw.

Supposed you have a user who is logined, and has certain attributes - name, nickname, followers, etc which is stored in the backend You would be using this attributes extensively in javascript display, and having a mirror object in javascript would be very helpful. How would you do this? I am sure it is a very common problem.

Javascript jquery ruby-on-rails ruby-on-rails-3 link|improve this question asked Dec 10 '10 at 10:24ming yeow2,85921536 88% accept rate.

Have a look at the js-data-helper plugin, which gives you rails helpers that dump data attributes into DOM elements. It hasn't been updated in a while, but it has lots of features and is well-documented. Also, if you want to go with a custom solution, you can get some pointers by seeing how it's done with this plugin.

This plugin looks great, although might be too complicated for my needs. But you are right, it is giving me a lot of the right ideas for designing a custom, simple version – ming yeow Dec 12 '10 at 2:55 Cool, just be careful about storing arrays and hashes. Those can be tricky to convert sometimes.

– bowsersenior Dec 12 '10 at 18:55.

Here's a way you could do it. Your application. Js file could make an AJAX call to a User controller that returns @current_user serialized to JSON.

Var currentUser; var success = function(user) { currentUser = user; } $. Get("/users/current/", success); In your controller: def current respond_to do |format| format. Js { render :json => @current_user } end end.

Store it in a global hash collection, by placing some javascript in 'views/layouts/application.html. Erb'. That would be my guess =).

That is what I am doing now, but it is too hard to maintain and keep consistent – ming yeow Dec 12 '10 at 2:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions