Search code examples
pebble-watchcloudpebblepebble-js

Pebble JavaScript Multiple JS Files (Pebble.js)


I'm creating a project on CloudPebble using JavaScript.

I have a "Constants.js" which hosts a variable that I would like to access using "app.js", which is the main contents of the app. However, running the app I receive the following error:

[PHONE] pebble-app.js:?: JavaScript Error:
TypeError: Cannot read property 'length' of undefined

Here is my code:

Constants.js

var mainMenuOptions = ["MenuOption1", "MenuOption2", "MenuOption3"];

app.js

var UI = require('ui');
var Vector2 = require('vector2');
var constants = require('Constants.js');

var mainMenu = new UI.Menu({
});

for (var i = 0; i < constants.mainMenuOptions.length; i++) { //Error occurs here
  mainMenu.item(0, i, { title: constants.mainMenuOptions[i] });
}
...

Any help is appreciated. Thanks!


Solution

  • I beleive your Constants.js should have this format:

    var Constants = {
       mainMenuOptions: ["MenuOption1", "MenuOption2", "MenuOption3"]
    };
    
    this.exports = Constants;
    

    And then in app.js do

    var constants = require('Constants');
    

    to access it.

    Used this approach in my very first Pebble.js app Autoinsult and it worked.