4 Replies Latest reply on Dec 17, 2012 11:23 AM by Jon Tara

    Can't figure out writing to a file from within a rhoelements app

      I have a local file that is read in upon the initial launch of my rhoelements app, but I want to be able to update that file during the app execution.  I'm not an expert in javascript or web development, but from what I've found, using javascript to write to local files is either frowned upon or impossible for security reasons.

       

      I've been trying to use the following resource in order to use html5 standards to write to a local file, but I can't seem to get this to work either.  http://www.html5rocks.com/en/tutorials/file/filesystem/ 

       

      Long story short, there must be an easier way to do file IO in Rhomobile than my current strategy, but I can't seem to find anything in the documentation about it nor can I seem to get any web-based approaches to work correctly.

       

      One of the goals of my project is to try to use as little rhomobile specific functions as possible, so if anyone has a more html/javascript solution available, that would be preferable to a rhodes solution.

       

      Edit: I should note that what I want to modify is completely offline.

       

      Edit2: I never did figure out how to do it without using Rhodes, but here's the resource I eventually had to use to get it to work, since I find some rhomobile documentation difficult to find.  http://docs.rhomobile.com/rhodes/device-caps#alerts

       

      If anyone finds a way to do the same thing without using Rhodes, I'd still like a reply.

        • Re: Can't figure out writing to a file from within a rhoelements app
          Dino Gregorich

          Brandon,

          I have a customer who is evaluating RhoMobile.  He is moving from a C#.net app to a RhoMobile app. He wants to read and write files like you desired in your original posting.  I am still learning RhoMobile and am way behind the learning curve.  Do you have a snippet of code from your application that you can share with regard to File I/O (local file reading and writing)?

           

          Thanks.

          -Dino

          • Re: Can't figure out writing to a file from within a rhoelements app
            Jon Tara

            This is a better link for how to write to the local filesystem:

             

            http://docs.rhomobile.com/rhodes/device-caps#file-system-access

             

            IMO, you are just asking for trouble to try to use just Javascript. This sort of thing is much easier in Rhodes, but, yes, you may need to learn a new language (Ruby). If you try to write your app in Javascript, you are leaving most of the power of Rhodes behind. For example, it is much easier to create dynamic pages using .erb files and a bit of Ruby code than it is to do it in Javascript.

             

            A parallel can be drawn to PhoneGap, since in some ways PhoneGap and RhoElements are competing technology. It's very easy to create a simple PhoneGap app that simply consists of some static HTML pages. Once you starting creating dynamic pages and adding business logic, it can become very tedious. I don't recommend having more than a couple of dynamic pages. I've been working on a couple of PhoneGap apps for clients that have, for example, a map and that keep track of an itinerary. I wouldn't want to try to create a PhoneGap app much more complicated than that, and would steer any client wanting something more than that away from PhoneGap and toward Rhodes. Rhodes, not RhoElements. Javascript just isn't that well-suited to the task.

             

            I know that RhoMobile is working toward making all device capabilities available in Javascript through RhoElements, but not all device capabilities are yet so exposed.

              • Re: Can't figure out writing to a file from within a rhoelements app
                Darryn Campbell

                Completely agree but just to add you can access the File API through JavaScript (from the API page at http://docs.rhomobile.com/rhodesapi/file-api/#using-javascript-api):

                You can call the Ruby File methods from JavaScript. To use the Javascript API, add thepublic/js/rho_javascript_api.js file to the .html, .erb, or .js file calling the JavaScript method.

                The JavaScript API methods with a return value can pass it as a parameter in jQuery-compatible continuation (deferred object, a kind of callback). Possible continuations to handle are done, fail, and complete.

                Rho.Class.method(params).done(function(handler) { /* handler... */ }) 

                You must enable JavaScript by putting rho-javascript into extensions in your build.yml.

                extensions: ["rho-javascript"] 

                You must have a RhoElements license to use the JavaScript API.

                  • Re: Can't figure out writing to a file from within a rhoelements app
                    Jon Tara

                    Mea culpa. Yes, there are Javascript functions to access the filesystem if you are using RhoElements with a license. These Javascript functions do not correspond, though, with the HTML5 FileSystem API. They simply expose the Ruby filesystem APIs available in Rhodes to Javascript. So, you will still need to learn the standard Ruby file I/O, although you won't need to learn Ruby itself.

                     

                    It should be possible to write a compatibility layer if you want to use the HTML5 FileSystem API.

                     

                    It would not be that hard to just learn the little bit of Ruby you need to write controller methods, and implement your own Rhodes controller methods to do the underlying file I/O and expose higher-level application-specific controller methods that you can use from Javascript using simple jQuery Ajax calls.

                     

                    You then would not need a RhoElements license. But, then again, if you intended to do your project in all (or mostly) Javascript then you are likely to need RhoElements in any case.