Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@InjectContext does not work with JavaViews #544

Open
nils-christian opened this issue Mar 30, 2018 · 3 comments
Open

@InjectContext does not work with JavaViews #544

nils-christian opened this issue Mar 30, 2018 · 3 comments
Labels
Milestone

Comments

@nils-christian
Copy link
Contributor

Hi,

Assume following view:

public class ViewA implements FxmlView<ViewAModel>, Initializable {

	@InjectContext
	private Context ivContext;

	@Override
	public void initialize( final URL aUrl, final ResourceBundle aResourceBundle ) {
		System.out.println( "Context is " + ivContext );
	}

}

In this scenario, the context is injected as expected. Now I convert the view into a JavaView:

public class ViewA extends VBox implements JavaView<ViewAModel>, Initializable {

	@InjectContext
	private Context ivContext;

	@Override
	public void initialize( final URL aUrl, final ResourceBundle aResourceBundle ) {
		System.out.println( "Context is " + ivContext );
	}

}

In this case, the context is null in the initialize method. This is very troublesome for me, as I have no FXML views, but only Java views.

I have a main view which creates further sub views (tabs) and needs to communicate with them via a Scope. The scope is available in the main view model, but is currently not transported to the sub view models, as mvvmFX cannot determine that the subviews are in the same hierarchy as the main view. Therefore I want to use the context of the main view as parent context so that the hierarchy is correct. With this bug this is currently not possible. As the scope cannot be injected into the main view either, I have no chance to create the sub views in the main view currently. Is there any possible workaround?

Thanks and best regards,

Nils

@manuel-mauky
Copy link
Collaborator

Hi,
you are right: Scopes aren't working with JavaViews at the moment. The scopes are still in "beta" state because we only have a limited number of applications that are using them and therefore only limited feedback yet. And in our applications we are using FXML for most views which is the reason why we have implemented them for FXML first.

Adding support for JavaViews is on our todo-list but it's not an easy task. I hope I can find the time to take a closer look at it withing the next days but I can't guarantee anything :-)

Until then using FXML based Views would be the best approach. Is there a reason why you can't use FXML? If you need to create your UI in Java-Code maybe you could still use a minimal FXML file that only consists of a container element like this:

<VBox fx:controller="com.example.MyView" fx:id="root"></VBox>
public class MyView implements FxmlView<MyViewModel> {
	@FXML
	private VBox root;

	public void initialize() {
		// use Java code to create the ui and add it to "root" container.
	}
}

@nils-christian
Copy link
Contributor Author

Hi,

Thank you. I understand that FXML views have a higher priority than the plain Java views for the mvvmFX project. I will try and use the approach with the minimal FXML for the moment, but we should keep this ticket still open.

It is not that I cannot use FXML, I just don't want to. In my opinion the FXML files have more disadvantages compared to views designed in Java.

Best regards,

Nils

@nils-christian
Copy link
Contributor Author

Hi,

From the looks of it - shouldn't it be enough to inject the context right after the injection of the view model in the JavaViewLoader, similar to the code in the FxmlViewLoader? A quick test seems to confirm this.

Best regards

Nils

@manuel-mauky manuel-mauky added this to the 1.9.0 milestone Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants