大家好,老师来了,老师又来了……幸好不收稿费也不收学费。所以一个ContextMenu就可以写n篇,大家也没有什么意见,这次老师要说的是……控制MenuItem的属性。应用场景如下:
针对目标,右键菜单可能启用一些菜单项,禁用一些菜单项。
简单的例子是,选中图片文件可以用Acdsee打开,选中word文件就是用word打开了。
这里的解决思路是从YUI 邮件组搜到的。所以邮件组是个好东西。但是还需要挖掘啊。上面Todd是这么说的。(前文没看,可能Todd刚和文中麦扣打笔战)
On Jan
30
,
2007
,
at
11
:
54
AM
,
Todd Kloots wrote:
Michael -
If you set the " lazyload " configuration property to
" true " for your ContextMenu , then its contents will
not get created and the menu itself will not be
rendered until the " contextmenu " event is actually
fired.
That said , if you're creating a ~ 1000 elements with
unique context menus , then , yes it would be better to
reuse a single ContextMenu instance and reuse it. You
can subscribe to your context menu's " showEvent " and
use the " contextEventTarget " to determine what child
node was the actual target of the " contextmenu " event
that triggered the display of the menu. You can then
use that target element to determine what content you
need to add or remove from the menu.
Make sense?
- Todd
Michael -
If you set the " lazyload " configuration property to
" true " for your ContextMenu , then its contents will
not get created and the menu itself will not be
rendered until the " contextmenu " event is actually
fired.
That said , if you're creating a ~ 1000 elements with
unique context menus , then , yes it would be better to
reuse a single ContextMenu instance and reuse it. You
can subscribe to your context menu's " showEvent " and
use the " contextEventTarget " to determine what child
node was the actual target of the " contextmenu " event
that triggered the display of the menu. You can then
use that target element to determine what content you
need to add or remove from the menu.
Make sense?
- Todd
上下文不重要,重点是这里说明了一个事情,menu是有showEvent这个事件的,往前追溯就是他老爸(的老爸) YAHOO.widget.Module有那么一个东西。继承给Menu-ContextMenu了。所以基于这个showEvent事件触发时,兄弟们去取menu.contextEventTarget。就能拿到目标的那个DOM对象了。
拿到这个对象后,就可以根据对象的数据(属性)来决定要不要显示一些菜单。代码如下。照旧片断.
先注册
oContextMenu.showEvent.subscribe(onContextMenuShow,
this
);
然后读取数据就行了。外面的鞭炮声和爆炸案有得一拼
function
onContextMenuShow()
...
{
disabledMenu(oContextMenu.contextEventTarget);
}
function disabledMenu(vo) ... {
//alert(oTarget.first);
if(vo.last||vo.dataType=='m')...{
oContextMenu.getItem(4).cfg.setProperty("disabled", true);
//alert(oContextMenu.getItem(3));
}else...{
oContextMenu.getItem(4).cfg.setProperty("disabled", false);
}
if(vo.first||vo.dataType=='m')...{
oContextMenu.getItem(3).cfg.setProperty("disabled", true);
}else...{
oContextMenu.getItem(3).cfg.setProperty("disabled", false);
}
}
disabledMenu(oContextMenu.contextEventTarget);
}
function disabledMenu(vo) ... {
//alert(oTarget.first);
if(vo.last||vo.dataType=='m')...{
oContextMenu.getItem(4).cfg.setProperty("disabled", true);
//alert(oContextMenu.getItem(3));
}else...{
oContextMenu.getItem(4).cfg.setProperty("disabled", false);
}
if(vo.first||vo.dataType=='m')...{
oContextMenu.getItem(3).cfg.setProperty("disabled", true);
}else...{
oContextMenu.getItem(3).cfg.setProperty("disabled", false);
}
}
问题就是这样解决的。