【发布时间】:2020-09-04 18:41:26
【问题描述】:
使用 GOJS 是否可以将绑定参数传递给函数?
我想使用这个传递的 var 来更改返回的字体类型组,例如在下面的代码中,aplha 参数被传递回调用 textStyle() 函数的节点,该函数将基于 if/else在传递的值上。
myDiagram.nodeTemplateMap.add("Source",
$(go.Node, "Auto", nodeStyle(),
$(go.Shape, "RoundedRectangle",
{ fill: bluegrad },
new go.Binding("fill", "color")
),
$(go.TextBlock, textStyle(alpha),
new go.Binding("text", "text"),
new go.Binding("alpha", "alpha")
),
makePort("T", go.Spot.Top, false, true),
makePort("L", go.Spot.Left, true, true),
makePort("R", go.Spot.Right, true, true),
makePort("B", go.Spot.Bottom, true, true)
));
//.....
myPalette =
$(go.Palette, "myPaletteDiv", // must name or refer to the DIV HTML element
{
nodeTemplateMap: myDiagram.nodeTemplateMap, // share the templates used by myDiagram
model: new go.GraphLinksModel([ // specify the contents of the Palette
{ category: "Source", text: "source", alpha:"fontGroupA"},
{ category: "Source", text: "source", alpha:"fontGroupB"},
])
});
//.....
// Common text styling
function textStyle(alpha) {
console.log("value passed:"+alpha);
return {
margin: 6,
wrap: go.TextBlock.WrapFit,
textAlign: "center",
editable: true,
font: bigfont
}
}
更新
根据下面 Walter 的回答,我添加了一个回调和绑定,现在可以使用了。
//.....
myPalette =
$(go.Palette, "myPaletteDiv", // must name or refer to the DIV HTML element
{
nodeTemplateMap: myDiagram.nodeTemplateMap, // share the templates used by myDiagram
model: new go.GraphLinksModel([ // specify the contents of the Palette
{ category: "Source", text: "source_a", css:"fontGroupA"},
{ category: "Source", text: "source_b", css:"fontGroupB"},
])
});
//...
$(go.TextBlock,
{
// Default settings //
font: "bold 15pt Helvetica, Arial, sans-serif",
stroke: lightText,
margin: 8,
maxSize: new go.Size(100, NaN),
wrap: go.TextBlock.WrapFit,
editable: true
},
new go.Binding("font", "css", function(v) {return cssStyle(v).font}).makeTwoWay(),
new go.Binding("stroke", "css", function(v) {return cssStyle(v).stroke}).makeTwoWay()
)
// ...
function cssStyle(theStyle) {
if (theStyle === "fontGroupA"){
return {
margin: 6,
wrap: go.TextBlock.WrapFit,
textAlign: "right",
editable: true,
font: bigfont,
stroke: "red"
}
}else if (theStyle === "fontGroupB"){
return {
margin: 6,
wrap: go.TextBlock.WrapFit,
textAlign: "left",
editable: true,
font: smallfont,
stroke: "blue"
}
}else {
return {
margin: 6,
wrap: go.TextBlock.WrapFit,
textAlign: "center",
editable: true,
font: midfont
}
}
【问题讨论】:
标签: gojs