Reputation: 14044
I been reading through Large scale application development and MVP and I was wondering how you guys deals with dynamically added components within this suggested way of implementing the pattern.
Lets say you have a table, where you have three buttons in each table row, and table rows can be added dynamically. How would you guys suggest binding these to the presenter?
The new row, if following Google's proposed structure, will get generated in the view, which does not have a direct link to the presenter and can not call back to it (so no calling bindNewButtons(Button, Button, Button)
-ish type method).
So, what's good practice here? I was thinking since the presenter will be handling the event which adds a new row in the widget inside the view (handles as in, fires the method in the view which generates this new row), I could have a getRowButtons(int index)
method in the view, and then using this to retrieve the components and bind them after they where added.
I'm sure there's a more clever way of doing this though, so I'm seeking a bit of advice here.
Upvotes: 1
Views: 904
Reputation: 3223
The second article in this series shows a view which does have a reference to the presenter.
The view:
private Presenter<T> presenter;
public void setPresenter(Presenter<T> presenter) {
this.presenter = presenter;
}
The presenter:
public interface Presenter<T> {
void onAddButtonClicked();
void onDeleteButtonClicked();
void onItemClicked(T clickedItem);
void onItemSelected(T selectedItem);
}
You could now define a method in the presenter interface which has a parameter which indicates which row was clicked. If you don't want to make the view aware of the presenter interface you could always choose to fire an event on the event bus which the presenter could respond to. Based on your question though, the first option seems a more reasonable answer.
Upvotes: 2