SproutCore 以编程方式触发菜单窗格中的项目选择
我们正在使用 SproutCore v1.6 并尝试以编程方式设置选定的菜单项。 MenuPane
如下所示。
OurPage = SC.Page.design({
myAccountMenu: SC.MenuPane.create({
layout: { width: 200 },
items: [
{ title: 'Sign Out', action: 'signOut' }
],
itemActionKey: 'action',
itemTitleKey: 'title'
})
});
然后我们尝试按如下方式设置“selectedItem”。
OurPage.getPath('myAccountMenu').set('selectedItem', OurPage.getPath('myAccountMenu').items[0])
这会正确设置所选项目,我们可以查询以下内容来获取该项目:
OurPage.getPath('myAccountMenu').get('selectedItem')
但是它实际上从未触发菜单上的操作。例如,它实际上并没有让用户退出。但是,当您手动单击菜单项时,它可以正常工作。
We are using SproutCore v1.6 and trying to programmatically set the selected menu item. The MenuPane
looks like the following.
OurPage = SC.Page.design({
myAccountMenu: SC.MenuPane.create({
layout: { width: 200 },
items: [
{ title: 'Sign Out', action: 'signOut' }
],
itemActionKey: 'action',
itemTitleKey: 'title'
})
});
We are then trying to set the 'selectedItem' as follows.
OurPage.getPath('myAccountMenu').set('selectedItem', OurPage.getPath('myAccountMenu').items[0])
This sets the selected item correctly and we can query the following to get the item:
OurPage.getPath('myAccountMenu').get('selectedItem')
However it never actually triggers the actions on the menu. e.g. It doesn't actually sign the user out. However when you manually click the menu item it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些事情
1) 使用页面上的插座来获取 accountMenu,
这样如果
myAccountMenu
的路径发生变化,您只需更改插座即可。2) 要设置项目,只需执行
3) 查看代码,以编程方式设置选择时没有简单的方法来调用操作。那是因为您传入的项目会变成
MenuItemView
实例,并且这些项目会在mouseUp
上触发其操作。但是,如果您使用状态图(或者即使没有),您只需从项目中获取操作并调用它
App.statechart.sendEvent(toSelect.action);
A few things
1) Use an outlet on your page to get the accountMenu
that way if the path to
myAccountMenu
changes, you only have to change your outlet.2) To set the item, just do
3) Looking at the code, there is no easy way to invoke the action when programmatically setting the selection. Thats because the items you pass in get turned into
MenuItemView
instances, and those fire their actions onmouseUp
.However, if you are using statecharts (or even if not), you can just grab the action off the item and invoke it
App.statechart.sendEvent(toSelect.action);