views:

122

answers:

2

Looking at the javafx tutorials and samples, bindings are always made during varibale declarations:

def y = bind x;

or

def address = Address {
  street: bind myStreet;
};

But what do I do, if I have an exisiting object - and hence don't declare it - and want to bind one of its attributes. In my case I load a SVGPath with the FXDLoader and then want to bind SVGPath.visible to a variable. How can I achieve this?

var data = true;
var fxdContent = FXDLoader.load("{__DIR__}plan.fxz");
var sc = fxdContent.lookup("SC0013") as SVGPath;
sc.visible = bind data; //That doesn't work
+2  A: 

You can use a replace trigger instead e.g.

var fxdContent = FXDLoader.load("{__DIR__}plan.fxz");
var sc = fxdContent.lookup("SC0013") as SVGPath;
var data = true on replace {
    sc.visible = data;
}

You might also be able to do:

def data = bind sc.visible with inverse;

This would give you bidirectional updates between the two variables. According to the language specification, you can't use "bind" anywhere else.

Craig Aspinall
thanks! that works! But nevertheless I'm interessed in the bind question.
räph
I updated the answer to include another possible solution and a link to the relevant section in the language specification.
Craig Aspinall
thanks for the second solution. didn't know that one. and I didn't knew the language spec either, only the stuff at javafx.com ;)
räph
A: 

Yes, you can bind a variable only when you are declaring it.

Honza