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

Simple notification from ViewModel to View #203

Closed
manuel-mauky opened this issue Apr 12, 2015 · 0 comments
Closed

Simple notification from ViewModel to View #203

manuel-mauky opened this issue Apr 12, 2015 · 0 comments
Assignees
Labels
Milestone

Comments

@manuel-mauky
Copy link
Collaborator

In the MVVM pattern the ViewModel may not have a dependency to the View but it can send notifications to the View.
An example use case is the triggering of an animation in the View. The Logic of when the animation should start is defined in the ViewModel and the definition of the animation is part of the View.

At the moment there are at least three ways of modeling this use case:

  1. Use a BooleanProperty in the ViewModel that switches to true when the animation should start. The downside of this approach is that this kind of modeling is quite inadequate: A BooleanProperty represents state but we need a signal.
  2. the View adds a callback on the ViewModel that the ViewModel can call. This approach needs a lot of code.
  3. Use the NotificationCenter. The downside of this approach is that the NotificationCenter is global and therefore better suited for communication between different components of the application. For our use case we would like to have a better encapsulated approach.

In this issue we should think about a really easy solution. I'm thinking of something like this:

public class MyViewModel implements ViewModel {
    public void someAction() {
        ...
        notifiy("trigger-animation"); 
    }
}

public class MyView implements FxmlView<ViewModel> {
    public void initialize() {
        subscribe("trigger-animation", () -> {
            // some code
        });
    }
}

The ViewModel interface defines a method notify(String key) with a default implementation. The FxmlView interface defines a method subscribe(String key, Callback callback). The framework takes care for the handling of the notifications.

@sialcasa sialcasa added this to the 1.2.0 milestone Apr 26, 2015
@sialcasa sialcasa self-assigned this Apr 26, 2015
manuel-mauky added a commit that referenced this issue Apr 27, 2015
manuel-mauky added a commit that referenced this issue Apr 27, 2015
@manuel-mauky manuel-mauky modified the milestones: 1.2.0, 1.3.0 Apr 30, 2015
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