【问题标题】:Pebble.JS: Acces a menu object created in a functionPebble.JS:访问在函数中创建的菜单对象
【发布时间】:2015-10-05 19:32:50
【问题描述】:

简述:

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

完整解释:

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

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

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

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

代码:

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.
}

【问题讨论】:

    标签: javascript function pebble-js


    【解决方案1】:

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

    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 + '"');
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      相关资源
      最近更新 更多