Pebble.JS: 访问在函数中创建的菜单对象

Pebble.JS: Acces a menu object created in a function

简介:

我在函数中创建了一个菜单对象,但我无法在主脚本中访问该菜单对象。

完整解释:

在函数 createMyMenu() 中,我确实创建了一个包含一些项目的菜单对象,并显示了菜单。这工作正常,用户可以使用 Pebble 智能手表上的按钮在菜单中移动 up/down。

问题是用户无法 select 任何项目,只能在菜单中导航。

如果我在主脚本中创建菜单对象,我(当然)工作得很好,但如果我在函数中创建菜单对象,用户不能select任何菜单项。

问题:如何在函数中创建菜单,然后在主脚本中使用该菜单对象,就像在主脚本中创建菜单一样?

代码:

function createMyMenu()
{
// Some code to create the menu object myMenu. Works fine.
  myMenu.show();  // Also works fine, the menu and it contents are displayed.
  return myMenu; // No errors
}

和脚本

mainMenu = createMyMenu(); // Create the menu.
mainMenu.on('select', function(e) // This does not seem to be executed. 
{
  // Code to execute when the user selects a menu item.
}

我自己解决了这个问题,所以这里有一些工作代码:

var UI = require('ui');

function createMyMenu()
{
// Some code to create the menu object myMenu. Works fine.
var myMenu = new UI.Menu({
  backgroundColor: 'black',
  textColor: 'white',
  highlightBackgroundColor: 'white',
  highlightTextColor: 'black',
  sections: [{
    title: 'My Menu',
    items: [{
      title: 'First Item',
      subtitle: 'first subtitle'
    }, { 
      title: 'second Item',
      subtitle: 'Second subtitle'
    }, {
      title: 'Third Item',
      subtitle: 'Third subtitle'
    }]
  }]
}); 

 myMenu.show();  // Also works fine, the menu and it contents are displayed.
  return myMenu; // No errors
}

//***** THE SCRIPT ******

var mainMenu = createMyMenu(); // Create the menu.
mainMenu.on('select', function(e) // This does not seem to be executed. 
{
  // Code to execute when the user selects a menu item.
    console.log('Button pressed');
    console.log('Selected item #' + e.itemIndex + ' of section #' + e.sectionIndex);
  console.log('The item is titled "' + e.item.title + '"');
});