0

I need to store a js object in a ruby class then pass it back to js later intact.

just for some context I have a fallback but its not efficient. I Can chop up the js object and pass the values as strings one by one and piece it back together but its a lot of data.

On my js side if i can keep the js object intact i can simply pass the object back to js and I'm set.

For further context what I'm trying to do is store an ace editor session ace.edit("editor").getSession() which returns QUITE alot of data as you can see below

enter image description here

inside of one of a Ruby class to pass back to my js later. The editor session contains ALOT of settings things like language mode , editor theme, font size toggle wrap you get the point all those settings you have in your editor on your env I need to store. Ive passed back the important ones already, things like language and theme but want to pass the whole session so the user-x is as close to working on the editor in your env as possible

I am brand new to js and just started diving into it on this project so please if you can when answered share some pseudo or a snippet I can analyze. Thanks ahead of time

8
  • Do you need to pass this js object to your server, process it and get it back to browser or do you just want to manually paste it to ruby to make some experiments? Commented Apr 29, 2018 at 21:06
  • Yes , need to pass it to server then pass it back as is no manipulation the only goal here is to store it inside of one of my models. To be exact I have a CodeSnippet class model every snippet has a Session with ace editor i can simply setSession(pass the js object here) so nothing needs to be done on the ruby side besides associating it to my model Commented Apr 29, 2018 at 21:16
  • You said you have working fallback. Can you paste here the code so I can update your snippet? It's too hard to write something abstract, without real samples Commented Apr 29, 2018 at 21:19
  • Ok so here's an example of how I get pass and reset 1 of the values from the js object. Here I use js to pass the mode value to an input in my form. In my controller I simply assign that value to my ruby class where I need it then later in my show view I pass it back and set the mode with a script inside the erb itself sorry pressed enter by accident before pasting the snippet give me a sec to get you the code Commented Apr 29, 2018 at 21:27
  • codeshare.io/alYOxB Commented Apr 29, 2018 at 21:36

1 Answer 1

1

JSON is a standard way to transfer complex data in form of string.

JS:

var editor = ace.edit("editor");

editor.getSession().on("change", function () {
    var session = ace.edit("editor").getSession();
    var neededData = {};
    neededData.mode = session.mode;
    neededData.fontSize = session.fontSize;
    //passing the mode to input
    $('input[name="mode"]').val(JSON.stringify(neededData));
});

Ruby:

editor_session_hash = JSON.parse(params[:mode])

Now you gonna get your whole session in a form of ruby hash. Your whole approach can be greatly improved, but it's totally another story

In a broad scope you should always serialize (you can google this term) your object into string on one side and then parse it on another side. There are several know formats of serialization, but for now you can stop on JSON and forget about all others

Sign up to request clarification or add additional context in comments.

7 Comments

awesome!!!!!!!!! and I dont doubt that haha I'm not even supposed to be playing with java-script yet in my curriculum just learned some simple methods and some syntax but thats about it everything has been pieced together from googles lol so I'm not to worried as far as my js goes just want the functionality and obviosly when I get to js and actually learn this stuff go back and refactor etc .. thanks so much!
came across this but not sure how to use github.com/douglascrockford/JSON-js/blob/master/cycle.js
It looks like ace.edit("editor").getSession() contains a reference on himself inside it's structure. I recommend you to create a new simple javascript object and put in this object everything you really need from session one by one. Then you can serialize this new object. And it will not have any cycle in it
Updated my answer
yea was afraid i'd have to piece it together .. cool , thanks from reading docs on that link looks like that library can handle this for me developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… i added the library just going to read up see how i can implement it if can't will piece it together
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.