views:

17

answers:

1

I'm working on a project that basically shall result in something like "source-to-source compiler" for javascript. Actually this is just the question if it shall result in some kind of compiler. Here's what I want to do:

I write Webapps in a generic way that shall be transformed into mobile device specific apps. So basically it's just like:

|Generic call| ====transformed to====> Device Specific Call

So I've got a set of generic calls I define (e.g. Foo.locateByGPS) that shall be transformed into code of the device specific native calls. So the order is the following:

  1. Write app: javascript mixed with own defined generic code

  2. Choose target device and give this app to the "compiler" that creates a hybrid app (native parts with web parts).

  3. Run it on the mobile device.

Beneath the generic code all the rest is standard javascript code that's running on all devices (respectively all browser/webviews on these devices).

Do I build a (source-to-source) compiler for this transformation?

I'm new to this topic, so I'm very thankful for some hints.

A: 

I wouldn't. If you use functions you can properly encapsulate all the platform specific code.

Now you've got 2 possibilities:

  • Create two script files that contain only the platform relevant code. -> You write the platform specific functions twice. The advantage is that you don't have to load the code for both platforms if you're only interested in one.

  • or use a if clause in every function. Ideally you could use feature-detection. This method seems to be more future-proof (more than 2 platforms?) and makes it easier to maintain the code. (Small changes are simpler to apply with all the code in one place.) Another advantage is that functions which are only partly different can share some code.

    function locateWithGPS() {
        if (navigator.geolocation) {
            // do something
        } else {
            // use a work-around
        }
    }
    
Georg
Thanks for your advice Georg, I will think about this approach and probably use it in the if-way...
zebarto