|
Re: E4 DI in "onAction" methods [message #1384680 is a reply to message #1384676] |
Fri, 30 May 2014 10:20 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Well you could declare your stuff as:
private Foo foo;
@Optional
@Inject
public void setFoo(Foo foo) {
this.foo = foo;
}
@FXML
public void handleSelection() {
if(foo != null) {
// ....
}
}
On 30.05.14 10:20, Uwe San wrote:
> Hi,
>
> I'm looking for a good solution for the following problem:
>
> Asssume you have an FXML file with a combo box and an associated
> onAction() method like <ComboBox ... onAction="#handleSelection" />
> The controller's handleSelection() method uses an instance of type Foo
> that was injected by E4 into the controller during its instantiation.
>
> Now assume you cannot guarantee there is a Foo instance available at
> instantiation time, so you would like to inject it into the
> handleSelection() method, as this is the point in time where you
> actually need the instance:
>
> @FXML
> @Inject
> public void handleSelection(Foo foo) {
> ..
> }
>
> As you might expect, this code cannot be parsed by the FXML loader:
> javafx.fxml.LoadException: Error resolving onAction='#handleSelection',
> either the event handler is not in the Namespace or there is an error in
> the script.
> What would be a good solution for this problem at the interface of
> JavaFX and E4? Of course, you could use ContextInjectionFactory.inject()
> or maybe call an E4 command programmatically, but these approaches don't
> seem very elegant.
>
> Thanks,
> Uwe
|
|
|
|
Powered by
FUDForum. Page generated in 0.03048 seconds