Example html dialog (suite)

ui::htmldialog

#21

Getting non-digits truncated typically happens when a string is parsed into a numeric value. When parsing it directly as Length this shouldn’t be an issue.


#22

It works perfectly fine for me. Just 2 lines of code for string replacement.


#23

And it stops working the second the user writes a ^ in a text field.


#24

Nope - already taken care of.


#25

Then again. Use json. Really. Whenever you serialise data, use a common serialization format.


#26

Nope - not interested - I’ve used my solutions since the early 80’s - no issues.

Why change something that isn’t broke (obviously rhetorical)


#27

Boy, not easy to find pertinent example code.


#28

https://www.w3schools.com/js/js_json.asp
http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html


#29

:woman_technologist:


#30

Yes, saw that. Thanks
Now struggling with read and write from local file.
Tried with Fetch. but of course, I got a Cross origin error.


#31

See:


Search results for “JSON” in Ruby API category


#32

I do think this has to be underlined once more. !!


#33

I’m just trying to get the Html dialog to show the last used values.

rb side
read values from local file √
make a hash with them √
hash.to_json √ json_str => {“thi”:“150”,“hei”:“2222”}.

Something is wrong in the following:

dialog.execute_script(“var jvals = #{json_str};”)

HTML side:
in a script

var Vals = JSON.parse(jvals)
document.getElementById(“thi”).value = Vals.thi
document.getElementById(‘hei’).value = Vals.hei


#34

I’m not an expert, but maybe you need quotes around the interpolated json_str when you pass it to jscript? like

dialog.execute_script(“var jvals = “#{json_str}””)


#36

Strings literals says it will get evaluated if the whole string is in “”

The irony here Steve, is that on Mac, I can just use an array. Pass it to html with a text sub and there it is seen as an array. Works pretty fine.
But not on pc.


#37

I run the following - works on both PC and Mac where data is a string.

script = "from_ruby_( '" << data << "' )"
dialog.execute_script(script)

#38

What if data is a string that contains ' ?


#39

Mine do not - I replace all single quotes with back quotes.


#40

@mariocha You do not really need the JS var jvals. Try …

dialog.execute_script("var Vals = JSON.parse('#{json_str}');")

The outer double quote is for Ruby interpolation.
The single quote around the JSON string is for JavaScript because JSON string data itself uses double quotes.

But you can also do …

dialog.execute_script("var jvals = '#{json_str}';")

#41

Thanks a lot Dan, I really needed a confirmation of one of my many tries.
working on the HTML side now.
Do I need to JSON.parse there ?
does the line. var jvals = ‘#{json_str}’; has to also be in the html ?