开发者

set icon for dijit.MenuItem

开发者 https://www.devze.com 2022-12-17 04:02 出处:网络
We have a case where we only know the icon for a menu item at runtime. I know there is a iconClass parameter for a diji.MenuItem, but this is of little help unless we dynamically add CSS rules at runt

We have a case where we only know the icon for a menu item at runtime. I know there is a iconClass parameter for a diji.MenuItem, but this is of little help unless we dynamically add CSS rules at runtime with dojox.html.insertCssRule - there must be a better way!

Here is an example of what we are trying to do:

pMenu = new dijit.Menu({
    targetNodeIds: ["NEW_APP"],
    leftClickToOpen: true
});

pMenu.popupDelay = 100;

pMenu.addChild(new dijit.PopupMenuItem({
    label: "clocks",
    iconSrc: "image/clocks.png",
    onClick: dojo.hitch(core.editor, core.editor.c开发者_如何转开发reateNewApp)
}));


Sure, there's a better way although not ideal, something like:

myMenuItem.iconNode.style.cssText = "background-image: url(...); width: 16px, height: 16px";


The reference to http://robrobbins.info/?p=372 is for an older version of dojo. In the newer syntax, a class can be defined as follows to do the same thing:


define("Foo/FooMenuItem", ['dojo', 'dijit/dijit', "dojo/_base/declare", "dijit/MenuItem"],
    function(dojo, dijit, declare, MenuItem) {

        return declare("Foo.FooMenuItem", [MenuItem], {
            iconSrc: "unknown", 
            _setIconSrcAttr: {node: "iconNode", type: "attribute", attribute: "src" }  
        });
    });

The simple Foo.FooMenuItem class can then just have the "icon" property set when the class is initialized, and the value set will be inserted in the img src filed for the icon. It can be referenced something like this:


pMenu.addChild(new Foo.FooMenuItem ({
    label: "clocks",
    iconSrc: "image/clocks.png",
    onClick: dojo.hitch(core.editor, core.editor.createNewApp)
}));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号