13 Replies Latest reply on Oct 10, 2013 7:11 PM by John Michael Vincent Rustia

    Need help with Javascript in RMS 4.0 Shared Runtime

    Sebastian Vajsman

      Hi all,

       

      we are trying to get started with RMS 4.0 Shared Runtime on a Motorola WT41N0 device.

      I read the API docs to setup some modules needed for our web app.

       

      The problem is, that none of the javascript code seems to be executed.

      E.g. trying to display indicator icons fails via javascript but works via meta tags.

       

      No icons are displayed:

        Rho.Battery.showIcon();
        batteryValues = Rho.Battery.batteryStatus();
        alert("Battery Level is: " + batteryValues['batteryLifePercent']);
      

       

      Icons are displayed:

      <META HTTP-Equiv="Battery"    Content="Left:260; Top:2; Color:#000000; Layout:Left; IconPosition:Right; GraphPosition:Top; Visibility:Visible">
      <META HTTP-Equiv="Signal"     Content="Left:2; Top:2; Color:#000000; Layout:Right; IconPosition:Left; GraphPosition:Top; Visibility:Visible">
      <META HTTP-Equiv="KeyState"   Content="Left:155; Top:2; Visibility:Visible">
      

       

      No matter what I try, it does not work. Just as KeyState and KeyCapture.

      Just as I add the Rho object to the source none of the javascript is executed (tested with e.g. alerts).

       

      Config.xml (extract)

            <NPAPI>
              <NPAPIDirectory value="file://%INSTALLDIR%\NPAPI\"/>
              <Preloads>
                <PreloadLegacyGeneric value="1"/>
                <PreloadLegacyODAX    value="1"/>
                <PreloadLegacyNoSIP   value="1"/>
                <PreloadLegacyAirBeam value="1"/>
                <PreloadLegacyAPD     value="1"/>
                <PreloadJSObjects     value="1"/>
              </Preloads>
            </NPAPI>
            
            <Preloads>
              <Preload value="Scanner"/>
              <Preload value="Hourglass"/>
              <Preload value="KeyCapture"/>
              <Preload value="SIP"/>
              <Preload value="ScreenOrientation"/>
            </Preloads>
      

       

      Any advice?

       

      rgds

      Sebastian

        • Re: Need help with Javascript in RMS 4.0 Shared Runtime

          Hi,

           

          Have you tried to include the rhoapi-modules.js in your external web server pages?

          You should extract this javascript from Rhostudio, you could follow this instructions

           

          Rhomobile | Using RhoMobile JavaScript API&rsquo;s

           

          This javascript contains the Rhoelements API such as Keystate, etc.

           

          This api allows us to access the device capabilities such as Network API,

          by just using javascript in our backend server, Rhomobile called this as Common API,

          since they are available both via ruby and javascript.

           

          Let me know if there are still problems, I would be glad to help.


          Regards,

          Vine

            • Re: Re: Need help with Javascript in RMS 4.0 Shared Runtime
              Sebastian Vajsman

              Hi Vine,

               

              thank you for your reply.

               

              As far as I understood the docs, you do not need to do this step using the Shared Runtime on Win CE devices,

              because JS modules are injected via NPAPI (see the extracted part  of config.xml).

               

              However I already tried it

               

              The test page I was fighting with is on the device (local):

              \Application\RhoElements\HTML\test.html
              

               

              The JS file is under:

              \Application\RhoElements\rho\apps\public\api\
              

               

              I already tried to address it relatively with:

              <script type="text/javascript" charset="utf-8" src="../rho/apps/public/api/rhoapi-modules.js"></script>
              

               

              But nothing seems to work.

               

              This one worked

              <META HTTP-Equiv="KeyCapture" content="KeyValue:0x0D; Dispatch:False; KeyEvent:url('http://www.motorola.com');">
              

               

              So KeyCapture seems to be preloaded at least.

              This only works for the ENTER Key.

               

              The F-Keys do not respond. Not even a simple JS alert...

               

              Regards,

              Sebastian

            • Re: Re: Need help with Javascript in RMS 4.0 Shared Runtime
              Robert Galvin

              The correct path to the a pi js file is just \public\api

                • Re: Re: Re: Need help with Javascript in RMS 4.0 Shared Runtime
                  Sebastian Vajsman

                  I'm a little bit confused regarding the JS api file and the shared runtime.

                  In the docs it says, that including the JS file is not necessary, because JS is injected through NPAPI.


                  <NPAPI>  
                          <NPAPIDirectory value="file://%INSTALLDIR%\NPAPI\"/>  
                          <Preloads>  
                            <PreloadLegacyGeneric value="1"/>  
                            <PreloadLegacyODAX    value="1"/>  
                            <PreloadLegacyNoSIP   value="1"/>  
                            <PreloadLegacyAirBeam value="1"/>  
                            <PreloadLegacyAPD     value="1"/>  
                            <PreloadJSObjects     value="1"/>  
                          </Preloads>  
                        </NPAPI> 
                  
                  

                   

                  Anyway if have to include the JS file in the HTML head, why is the path just

                  \public\api
                  

                   

                  when my html file lays in

                  \Application\RhoElements\HTML\
                  

                   

                  Imho there must be a difference between a native app and the shared runtime regarding the inclusion of the js api file.

                • Re: Need help with Javascript in RMS 4.0 Shared Runtime
                  Ian Hatton

                  I have the same issue in getting any JS to work - would someone be able to post a simple working example app at the GitHub rho-samples repository ?

                  • Re: Need help with Javascript in RMS 4.0 Shared Runtime
                    Robert Galvin

                    Make sure you put the rhoapi-modules.js before JQM

                     

                    <script src="/public/api/rhoapi-modules.js" type="text/javascript"></script>

                    <script src="/public/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>

                     

                     




                    • Re: Need help with Javascript in RMS 4.0 Shared Runtime
                      Derek Warren

                      I tested the code you provided and was getting some errors, for missing parameters etc. I wrote-up a sample page that should load at least the battery portion correctly through JS.

                       

                      This looks to be either a documentation issue or an incorrect API implementation, as the code samples @ Rhomobile | Battery show no parameters for the showIcon() and batteryStatus() methods; however, if I implemented the methods as in the samples it wouldn't work until I added at least 1 parameter. More testing to be done, tested w/ MC3190 WM6.5.

                       

                      <HTML>

                      <HEAD>

                      <script type="text/javascript" charset="utf-8" src="PATH TO MY rhoapi-modules.js"></script>

                      <script type="text/javascript">

                         function finLoad() {

                            Rho.Battery.showIcon({color:'#0000FF',layout:'up'});

                            Rho.Battery.batteryStatus({}, display);

                            //alert("Battery level is " + batteryValues['batteryLifePercent']);

                         }

                       

                       

                        function display(params){

                          alert("Battery level is " + params['batteryLifePercent']);

                        }

                      </script>

                      </HEAD>

                       

                       

                         <BODY>

                            <div id="htmDiv">

                            <p>Hey!</p><p>Hey!</p><p>Hey!</p><button onclick="finLoad()">Battery Status</button>

                            </div>

                         </BODY>

                      </HTML>

                        • Re: Re: Need help with Javascript in RMS 4.0 Shared Runtime
                          Sebastian Vajsman

                          Hi Derek,

                           

                          even this does NOT work... I'm freaking out!

                           

                          <html>

                          <head>

                          <script src="/public/api/rhoapi-modules.js" type="text/javascript"></script>

                          <script type="text/javascript">

                            function bestFunctionEver(){

                            Rho.Battery.batteryStatus({}, display);

                            }

                            function display(batteryValues){

                            alert("Battery Level is: " + batteryValues['batteryLifePercent']);

                            }

                          </script>

                          </head>

                          <body>

                            <div>

                                  <a href="#" onClick="JavaScript:bestFunctionEver();">Hit Me!</a>

                              </div>

                          </body>

                          </html>

                           

                          Maybe it is a WT41N0 or WIN CE 7 specific issue.

                          I'll try testing on a MC3190/CE6 device later.

                           

                          Regards

                          Sebastian

                        • Re: Need help with Javascript in RMS 4.0 Shared Runtime
                          Sebastian Vajsman

                          Ok I got it... at least Hello World...

                           

                          <html>

                          <head>

                          <META HTTP-Equiv="KeyState" Content="Left:155; Top:2; Visibility:Visible">

                          <script src="../rho/apps/public/api/rhoapi-modules.js" type="text/javascript"></script>

                          <script type="text/javascript">

                            function test(){

                            Rho.Battery.batteryStatus({}, display);

                            }

                           

                            function display(batteryValues){

                            alert("Battery Level is: " + batteryValues['batteryLifePercent']);

                            }

                          </script>

                          </head>

                          <body>

                              <div>

                            <a href="#" onclick="Javascript:test();">hit me hard!</a>

                            </div>

                          </body>

                          </html>

                           

                          This works.

                           

                          Downloaded the official RMS 4.0 from Motorola Website and redeployed RE on the mobile device.

                          Then i got the following log message:

                           

                          I 01/01/2009 00:08:18:000 00a6018e RhoWKBrowserEngine.cpp: 558| JS Log: file:///Program%20Files/RhoElements/HTML/test.htm, line 7: ReferenceError: Can't find variable: Rho

                           

                          Added this line to test.htm

                           

                          <script src="../rho/apps/public/api/rhoapi-modules.js" type="text/javascript"></script>

                           

                          And it works... I get the JS alert popup.

                           

                          BUT... I get it all 5 seconds (as the battery refresh rate is set to 5000ms)

                           

                          Why is the function triggered all 5000ms automatically?

                          In this case I would have thought it is executed only on click of the link.

                          How can I achieve this behaviour?

                           

                          Regards

                          Sebastian

                           

                          Happy anyways for reaching Hello World milestone

                            • Re: Re: Need help with Javascript in RMS 4.0 Shared Runtime
                              Benedict Kennedy

                              Hello Sebastian,

                              BUT... I get it all 5 seconds (as the battery refresh rate is set to 5000ms)

                               

                              Why is the function triggered all 5000ms automatically?

                              In this case I would have thought it is executed only on click of the link.

                              How can I achieve this behaviour?

                               

                              The Battery module was designed to continuously notify the web page of the battery level, this way the page can:

                              1. Use this information for it's own custom Battery icon/status report
                              2. Do actions at specific levels (for example, send all unsaved data to the server when the battery goes below 10%)


                              But of course, there will be times where you may want a one off reading just like what you are wanting. To do this, you need to stop the battery updates once you have received one event. This can be done with the JavaScript method:

                               

                              Rho.Battery.stopBatteryStatus();
                              

                               

                              To fit this into your program, add it to the "display(...)" function:

                               

                              <html>
                              <head>
                                <META HTTP-Equiv="KeyState" Content="Left:155; Top:2; Visibility:Visible">
                                <script src="../rho/apps/public/api/rhoapi-modules.js" type="text/javascript"></script>
                                <script type="text/javascript">
                                  function test(){
                                    Rho.Battery.batteryStatus({}, display);
                                  }
                              
                                  function display(batteryValues){
                                    alert("Battery Level is: " + batteryValues['batteryLifePercent']);
                                    Rho.Battery.stopBatteryStatus(); //Add this line
                                  }
                                </script>
                              </head>
                              <body>
                                <div>
                                  <a href="#" onclick="Javascript:test();">hit me hard!</a>
                                </div>
                              <body>
                              </html>
                              

                               

                              Best regards,

                              Ben

                              1 of 1 people found this helpful