0

I am unable to access ruby variables from JavaScript in tideSDK applications. The SDK version I am using, is the currently latest version of 1.2.0.RC4. Here is a basic example:

<!DOCTYPE html>
<html>
<head>
  <title>Hello World</title>
  <style type="text/css">
    body {background: #fff;}
  </style>
</head>
<body>
  <h1>Hello World</h1>

  <script type="text/python">
      helloP = "Hello from Python!";
  </script>

  <script type="text/ruby">
      $helloR = "Hello from Ruby!";
  </script>

  <script type="text/php">
      $helloPHP = "Hello from PHP!";
  </script>

  <script type="text/javascript">  
      alert("Hello from JavaScript!");
      alert(helloP);
      alert(helloPHP);
      alert(helloR);
  </script>
</body>
</html>

In this example, I have declared and assigned values to python, PHP and ruby variables. JavaScript alert() function works with the python and PHP variables, but not with the ruby variable. So, alert(helloP); and alert(helloPHP); work and display a pop up dialog with the contents of those variables, but nothing is displayed with alert(helloR).

On the other hand, ruby functions can be seen by JavaScript. So, if ruby_function is a ruby function, in JavaScript alert( ruby_function() ) works.

So, how can JavaScript see ruby variables? Any suggestions?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
sp2012
  • 133
  • 8
  • Checked this on Win7x64 SDK 1.2.0RC6d - can confirm this. Both the helloP and HelloPHP variables became properties of the window object, the helloR variable doesn't. Looks to me like some kroll bug. – Christian Engel Sep 10 '12 at 17:39

1 Answers1

2

This will not work in 1.2 release.

You need to have the ruby in an include file, not inlined.

Then it works as expected.

In Resources folder: index.html

<html>
    <head>
        <script>
         Titanium.include("ruby.rb");
        </script>
    </head>
<body style="background-color:#ccc;margin:0">
</body>
</html>
<script>
  console.log(window.crim);
  console.log(crim);
  alert(crim.foo);
</script>

in Resources folder: ruby.rb

window.crim = {
  'foo' => 'bar'
}

Please see https://wiki.appcelerator.org/display/guides/Using+Titanium+Desktop+with+Ruby - look for the info under the heading Titanium Desktop 1.2.0.RC1 SDKs and more recent.

meeech
  • 1,396
  • 13
  • 21