Jakob Lnr
Jakob Lnr

Reputation: 145

PhoneGap CoffeeScript Hello World

I´m new to PhoneGap/CoffeeScript trying to get a Hello World App running in iOS and wonder what I´m doing wrong.

This would be my standard index.html:

<!DOCTYPE html>
<html>
  <head>
  <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
  <meta charset="utf-8">


  <script type="text/javascript" charset="utf-8" src="cordova-1.6.0.js"></script>
  <script type="text/javascript" charset="utf-8" src="app.js"></script>

  </head>
  <body onload="onBodyLoad()">
  <h1>Hey, it's Cordova!</h1>
  <p>Don't know how to get started? Check out our <em><a target="_blank" href="http://docs.phonegap.com/en/edge/guide_getting-started_ios_index.md.html#Getting%20Started%20with%20iOS">Getting Started Guide</a></em>

  </body>
</html>

And the app.js generated from app.coffee looks like this:

(function() {
    var onBodyLoad, onDeviceReady;

    onBodyLoad = function() {
        return document.addEventListener("deviceready", onDeviceReady, false);
    };

    onDeviceReady = function() {
        return navigator.notification.alert("This alert never happens!");
    };

}).call(this);

When I delete the first line "(function() {" and the last line "}).call(this);" of the app.js get the alert and everything is working properly. But I guess there´s a better way than deleting this lines everytime CoffeeScript compiles into JavaScript.

thank you very much, Jakob

Upvotes: 2

Views: 3650

Answers (2)

Joscha
Joscha

Reputation: 4693

CoffeeScript offers a bare option -b which disables the wrapping.

Upvotes: 1

Buck Doyle
Buck Doyle

Reputation: 6397

Coffeescript wraps each file in a function to prevent it from polluting the global namespace. If you really want to do it this way, you could change the declaration of onBodyLoad to window.onBodyLoad, but best practices explicitly oppose placing Javascript calls in HTML elements like onload.

This is a variation on a popular question. Here is an example of how you can use an event listener to unobtrusively have your function called when the page has loaded.

Upvotes: 3

Related Questions