使用默认的instanciated形状扩展dojo.gfx.Group

时间:2009-12-09 00:10:30

标签: oop dojo dojox.gfx

我正在尝试使用dojo.gfx创建一些简单的UI组件。我已经设法扩展了dojo.gfx.Group,但是我已经深入了解了绘制到表面的任何默认形状。在Firebug中检查渲染的SVG,有一个节点但没有rect。

简化类看起来像这样:

dojo.provide("gfxui.SimpleButton");

dojo.require("dojox.gfx.shape");//-¿ needed?
dojo.require("dojox.gfx.svg");
dojo.require("dojox.gfx._base");

dojo.declare("gfxui.SimpleButton", dojox.gfx.Group, {
    constructor: function(){
        this.draw();
    },
    draw:function(){
        var bg = this.createRect(this.rect_props);
        //var bg = this.createObject(dojox.gfx.Rect);
    }
}

gfxui.SimpleButton.nodeType = dojox.gfx.Group.nodeType;

dojo.extend(dojox.gfx.Surface, {
    createButton: function(){
        var button = this.createObject(gfxui.SimpleButton, null, true);
        this.add(button);
        return button;
    }
});

HTML中的javascript如下所示:

dojo.require("dojox.gfx");
dojo.require("gfxui.SimpleButton");

function init(){
    var g = dojox.gfx;
    var surface = dojox.gfx.createSurface(dojo.byId("gfx_holder"), 800, 280, "#eee");
    var button = container.createButton();
};
dojo.addOnLoad(init);

1 个答案:

答案 0 :(得分:0)

我更喜欢简单的增强技术。以下是脚本标记的内容:

// let's include gfx (renderer will be selected dynamically)
dojo.require("dojox.gfx");

// useful short names
var d = dojo, g = dojox.gfx;

// our creator function
function createButton(color){
  // let's create our main shape: group
  var group = this.createGroup();
  // add custom properties, if any
  group._rect = group.createRect().
    setShape({x: 5, y: 5, width: 100, height: 30}).
    setStroke("black").
    setFill(color);
  return group;
}

// we want it to be available on groups and surfaces
d.extend(g.Surface, {createButton: createButton});
d.extend(g.Group,   {createButton: createButton});

// let's test the result
dojo.addOnLoad(function(){
  var s = g.createSurface(dojo.byId("surface"), 500, 400),
      b = s.createButton("red");
});

上面的示例假设有<div>称为“表面”。

扩充技术适用于任何渲染器,无论其实现如何,并且仅使用已发布的API。