Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Binding

Hierarchy

  • Binding

A Binding describes how to automatically set a property on a GraphObject to a value of a property of data in the model. The target property name and the data source property name are strings. All name matching is case-sensitive.

Register bindings by calling GraphObject.bind with a new Binding. Existing bindings become read-only, and no new bindings may be added, when a template (a Part) is copied. Bindings will be shared by all copies of the template's GraphObjects.

For example, your node data might be like:

{ key: 23, say: "hello!" }

Your simple node template might be like:

  var template = new go.Node(go.Panel.Auto);
// . . . define the rest of the Node's visual tree . . .
var txt = new go.TextBlock();
txt.bind(new go.Binding("text", "say"));
template.add(txt);
myDiagram.nodeTemplate = template;

Using GraphObject.make it might look like:

  var $ = go.GraphObject.make;
myDiagram.nodeTemplate =
$(go.Node, "Auto",
. . .
$(go.TextBlock, new go.Binding("text", "say"))
)

The data binding causes the TextBlock.text property of the TextBlock to be set to the value of the data's "say" property. If the value of the "say" property of a particular data object is undefined, the binding is not evaluated: the target property is not set. If there is an error with the binding, you may see a message in the console log. For this reason you may want to explicitly set the initial value for a property when defining the GraphObject, since that value will remain as the default value if the Binding is not evaluated.

Bindings are not necessarily evaluated in any particular order. Binding sources should not be (or depend in a conversion function on) the category of the data if you might be modifying the category (e.g. by calling Model.setCategoryForNodeData), because then some bindings might be evaluated before or after the category has been changed.

Conversions

Sometimes the data value needs to be modified or converted in order to be used as the new value of a GraphObject property. The most common conversion functions are provided for you -- they convert a string to a geometric class: Point.parse, Size.parse, Rect.parse, Margin.parse, Spot.parse, and Geometry.parse. But you can easily define your own conversion function.

As an example of a conversion function, let's use a function that adds some text prefixing the data property value:

  new go.Binding("text", "say", v => "I say: " + v)

Although simple conversions cover almost all binding cases, there are some infrequent uses that are covered by "Advanced Conversions", discussed below. Conversion functions must not have any side-effects. Conversion functions may be called frequently, so they should be fast and avoid allocating memory. The order in which conversion functions are called is not specified and may vary.

OneWay and TwoWay Bindings

By default bindings are Binding.OneWay. OneWay bindings are evaluated when the Panel.data property is set or when you call Panel.updateTargetBindings or Model.setDataProperty. OneWay bindings only transfer values from the source to the target.

TwoWay bindings are evaluated in the source-to-target direction just as OneWay bindings are evaluated. However when the GraphObject target property is set, the TwoWay bindings are evaluated in the target-to-source direction. There is no point in having a TwoWay binding on a GraphObject property that cannot be set. For efficiency, avoid TwoWay bindings on GraphObject properties that do not change value in your app.

You should not have a TwoWay binding with a source that is a node data object's key property, i.e. on the data property whose name is the same as the value of Model.nodeKeyProperty. Unintentionally changing the node key value to be the same as another node data's key value may cause indeterminate behavior. Furthermore, changing a node data key without changing any references to that node using the key value will result in "dangling" references and inconsistent relationships. You can make that change safely by calling Model.setKeyForNodeData, but not via a data binding.

The target-to-source update can also go through a conversion function. The most common back-conversion functions are provided for you. They convert a geometric class to a string: Point.stringify, Size.stringify, Rect.stringify, Margin.stringify, Spot.stringify, and Geometry.stringify.

It is common to want to update some data properties based on changes to the diagram. For example, as the user changes the Part.location by dragging a Node, you can automatically keep the node's model data in sync using a TwoWay binding.

  new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify)

The call to Binding.makeTwoWay changes the Binding.mode to be Binding.TwoWay and specifies the Binding.backConverter function to be the Point.stringify static function.

Because the Binding is on the whole node (template), the target object is the whole Node and the target property is "location". The value of data.loc will be a string representation of the Node.location value.

Binding Sources

The target of a Binding is always a property of a GraphObject or a RowColumnDefinition. The source of a Binding is normally a property of a data object in the model. But it is also possible to have the source of a Binding be the shared JavaScript object that is the value of Model.modelData. You can specify such a binding by calling Binding.ofModel, meaning "a binding of a source that is a property of the Model.modelData".

As an example, you might want all Nodes to use the same color. It would be possible but not natural to bind to a property on the node data object, because that property would have to be duplicated on all of the node data objects in the model, and updating the property would mean calling Model.setDataProperty on each node data object with the same new value. Furthermore if there happened to be no nodes at all in the model, there would be no place to save the data. Hence using the shared Model.modelData object would be the sensible place for that shared information.

  new go.Binding("stroke", "strokeColor").ofModel()

and to set or modify that color one would just call, within a transaction:

  model.setDataProperty(model.modelData, "strokeColor", "red");

That would cause all nodes with that model data binding to be re-evaluated. It is not commonplace to have a TwoWay Binding on "ofModel" Bindings, but that should work. Converters also work with "ofModel" Bindings.

And it is also possible to have the source of a Binding be another GraphObject that is in the same Part. You can enable such a binding by calling Binding.ofObject, meaning "a binding of a source that is a property of a GraphObject". You just have to make sure that object has a unique GraphObject.name or is the Part itself. The source property on the GraphObject has to be settable, and the Part must have a value for Panel.data. (If the source property setter does not notify about property value changes, the binding mechanism will not be invoked. Similarly, if there is no Panel.data, the binding mechanism is not active.)

As a common kind of example of data binding between two properties of GraphObjects, consider this Binding on a Shape which changes the color of the Shape.stroke depending on whether the Node is selected (Part.isSelected):

  new go.Binding("stroke", "isSelected", s => s ? "dodgerblue" : "gray").ofObject()

Note the call to Binding.ofObject, which tells the Binding that it should use as the source a GraphObject with a particular name. However that name argument is optional -- supplying no name (or supplying an empty string) will cause the binding to operate with the root GraphObject. In this case that would be the Node itself. Now with this binding whenever the value of Part.isSelected changes, this Shape's stroke changes color. The conversion function is what changes the boolean "isSelected" value to a brush color specifier.

Advanced Conversions

The binding functionality also has more advanced features for less common situations. The source property name may be an empty string, to convert the object as a whole. Conversion functions may take a second argument that takes the object that is bound. For source-to-target conversions, the second argument will be the GraphObject whose property is bound. For target-to-source (back-)conversions, the second argument will be the source data object and the third argument will be the Model.

Here's an example of a two-way data-binding using two custom conversion functions working with two separate data properties. First we define the two conversion functions.

  function toLocation(data, node) {
return new go.Point(data.x, data.y);
}

function fromLocation(loc, data, model) {
model.setDataProperty(data, "x", loc.x);
model.setDataProperty(data, "y", loc.y);
}

Then to data-bind the default template's Part.location property to two separate data properties, "x" and "y":

  new go.Binding("location", "", toLocation).makeTwoWay(fromLocation)

An empty string argument for the sourceprop parameter indicates that the whole data object should be passed to the toLocation function, rather than the value of some property of that data. The return value is used as the new value for the Part.location property. In almost all cases the second argument is not used. Caution: for efficiency reasons you should try to avoid using an empty source property name. Such bindings will be evaluated much more frequently than ones whose source is a particular property name.

The binding works normally for the source-to-target direction. But when the target property is modified it is the source property that is set with the back-converted property value from the target object. Because in this example the source property name is the empty string, and because one cannot replace the whole source data object, any return value from the conversion function is ignored. Instead the conversion function has to modify the data object directly, as this example fromLocation function does.

Note that because the source property name is the empty string, the binding system will not know which properties are modified in the call to fromLocation. Hence to support undo and redo, in order to make the data changes we have to call Model.setDataProperty so that the UndoManager can record the change, including the previous value.

Replacing Items in Arrays

However, although a TwoWay Binding cannot replace the node data object in the Model.nodeDataArray, it is possible to replace an item in an Panel.itemArray. So if your node data were:
  { key: 1, items: ["one", "two", "three"] }

And if your node template included something like:

    $(go.Panel, "Vertical",
new go.Binding("itemArray", "items"),
{
itemTemplate:
$(go.Panel,
$(go.TextBlock, { editable: true },
new go.Binding("text", "").makeTwoWay())
)
}
)

Then the user would be able to edit any of the TextBlocks, causing the item Array to be modified, for example resulting in this node data:

  { key: 1, items: ["one", "SOME NEW TEXT HERE", "three"] }

Index

Constructors

  • new Binding(targetprop?: string, sourceprop?: string, conv?: (val: any, targetObj: any) => any, backconv?: (val: any, sourceData: any, model: Model) => any): Binding
  • The constructor creates a one-way binding.

    Parameters

    • Optional targetprop: string

      A string naming the target property on the target object. This should not be the empty string.

    • Optional sourceprop: string

      A string naming the source property on the bound data object. If this is the empty string, the whole Panel.data object is used. If this argument is not supplied, the source property is assumed to be the same as the target property.

    • Optional conv: (val: any, targetObj: any) => any

      A side-effect-free function converting the data property value to the value to set the target property. If the function is null or not supplied, no conversion takes place.

        • (val: any, targetObj: any): any
        • Parameters

          • val: any
          • targetObj: any

          Returns any

    • Optional backconv: (val: any, sourceData: any, model: Model) => any

      An optional conversion function to convert property values back to data values. Specifying this function or specifying null modifies this binding to set its mode to be Binding.TwoWay. If you do not want a two-way binding, leave this argument unspecified (undefined).

        • (val: any, sourceData: any, model: Model): any
        • Parameters

          • val: any
          • sourceData: any
          • model: Model

          Returns any

    Returns Binding

Properties

  • Gets or sets a converter function to apply to the GraphObject property value in order to produce the value to set to a data property. This conversion function is only used in a TwoWay binding, when transferring a value from the target to the source. The default value is null -- no conversion takes place. Otherwise the value should be a function that takes one or two arguments and returns the desired value. However, the return value is ignored when the sourceProperty is the empty string.

    Conversion functions must not have any side-effects other than setting the source property.

    The function is passed the value from the target (the first argument), the source Panel.data object (the second argument), and the Model (the third argument). If the sourceProperty is a property name, that property is set to the function's return value. If the sourceProperty is the empty string, the function should modify the second argument, which will be the source data object.

  • Gets or sets a converter function to apply to the data property value in order to produce the value to set to the target property. This conversion function is used in both OneWay and TwoWay bindings, when transferring a value from the source to the target. The default value is null -- no conversion takes place. Otherwise the value should be a function that takes one or two arguments and returns the desired value. However, the return value is ignored when the targetProperty is the empty string.

    Conversion functions must not have any side-effects other than setting the target property. In particular you should not try to modify the structure of the visual tree in the target GraphObject's Part's visual tree.

    The function is passed the value from the source (the first argument) and the target GraphObject (the second argument). If the targetProperty is a property name, that property is set to the function's return value. If the targetProperty is the empty string, the function should set a property on the second argument, which will be the target GraphObject.

  • Gets or sets whether the source data is Model.modelData rather than a node data or link data object in the model. The default value is false -- the source data object will not be the shared Model.modelData object.

    see

    ofModel

    since

    1.7

  • Gets or sets the directions and frequency in which the binding may be evaluated. The default value is Binding.OneWay. Binding.TwoWay is the other choice.

    Use OneWay bindings to initialize GraphObject properties based on model data, or to modify GraphObject properties when the model data changes with a call to Model.setDataProperty. Use TwoWay bindings to keep model data in sync with changes to GraphObject properties. For efficiency, avoid TwoWay bindings on GraphObject properties that do not change value in your app.

    You should not have a TwoWay binding on a node data object's key property.

  • Gets or sets the name of the GraphObject that should act as a source object whose property should be gotten by this data binding. The default value is null, which uses the bound Panel.data as the source. If the value is a string, it should be the name of a GraphObject in the visual tree of the Panel that is bound to the data. Use the empty string to refer to the root panel, which is typically the whole Node or Link, but will be a Panel if used in a Panel.itemTemplate. The name must not contain a period.

    Binding only works if the source property is settable, not on computed or read-only properties, and if it supports notification. The documentation for the GraphObject (or subclass of GraphObject) property will indicate if the property is settable and if it does not notify.

  • Gets or sets the name of the property to get from the bound data object, the value of Panel.data. The default value is the empty string, which results in setting the target property to the whole data object, rather than to a property value of the data object. If sourceName is not null, then this property names the settable property on the GraphObject or RowColumnDefinition that acts as the source.

  • Gets or sets the name of the property to be set on the target GraphObject. The default value is the empty string; you should set this to be the name of a property.

Methods

  • Create a copy of this Binding, with the same property values.

    Returns Binding

  • makeTwoWay(backconv?: (val: any, sourceData: any, model: Model) => any): Binding
  • Modify this Binding to set its mode to be Binding.TwoWay, and provide an optional conversion function to convert GraphObject property values back to data values, as the value of backConverter.

    Use TwoWay bindings to keep model data in sync with changes to GraphObject properties. For efficiency, avoid TwoWay bindings on GraphObject properties that do not change value in your app. It is typical only to use TwoWay bindings on properties that are modified by tools or commands. Examples include Part.location by DraggingTool and TextBlock.text by TextEditingTool (only if TextBlock.editable is true).

    You should not have a TwoWay binding on a node data object's key property.

    Parameters

    • Optional backconv: (val: any, sourceData: any, model: Model) => any
        • (val: any, sourceData: any, model: Model): any
        • Parameters

          • val: any
          • sourceData: any
          • model: Model

          Returns any

    Returns Binding

    this two-way Binding.

  • ofObject(srcname?: string): Binding
  • Modify this Binding to set its sourceName property so as to identify a GraphObject in the visual tree of the bound Panel as the data source, instead of the Panel.data as the data source.

    This permits data binding on GraphObject properties, such as Part.isSelected. Remember that you can reliably data bind only on settable properties, not on read-only or computed properties.

    see

    sourceName, ofModel

    Parameters

    • Optional srcname: string

      the GraphObject.name of an element in the visual tree of the bound Panel; use an empty string to refer to the root panel of that visual tree, whose Panel.data is the bound data.

    Returns Binding

    this Binding to another GraphObject.

  • parseEnum(ctor: any, defval: EnumValue): (name: string) => EnumValue
  • This static function can be used to create a function that parses a string into an enumerated value, given the class that the enumeration values are defined on and a default value if the string cannot be parsed successfully.

    The normal usage is to pass the result of this function as the conversion function of a Binding.

    linktemplate.bind(new go.Binding('routing', 'dataPropName', go.Binding.parseEnum(go.Link, go.Link.Normal)));
    

    This binding will try to parse the string that is the value of the bound data's "dataPropName" property. If it is a legitimate enumerated value defined on the Link class, the conversion function will return that value. If the bound data's "dataPropName" property is not present or has an unrecognized value, the Link.routing property gets the default value, Link.Normal.

    Parameters

    • ctor: any

      the class constructor that defines the enumerated values that are being parsed.

    • defval: EnumValue

      the default enumerated value to return if it fails to parse the given string.

    Returns (name: string) => EnumValue

    a function that takes a string and returns an enumerated value.

      • (name: string): EnumValue
      • Parameters

        • name: string

        Returns EnumValue

  • toString(val?: any): string
  • This static function can be used to convert an object to a string, looking for commonly defined data properties, such as "text", "name", "key", or "id". If none are found, this just calls toString() on it.

    Parameters

    • Optional val: any

    Returns string

Constants

This value for Binding.mode uses data source values and sets GraphObject properties. Bindings are evaluated when Panel.updateTargetBindings is called.

This value for Binding.mode uses data source values and GraphObject properties and keeps them in sync. When Panel.updateTargetBindings is called, the GraphObject properties are set. When GraphObject properties are modified, the Panel.data properties are set.