Domanda

I have the following GWT module:

public class FizzModule implements EntryPoint {
    private Buzz buzz;

    public FizzModule() {
        this(null);
    }

    public FizzModule(Buzz bz) {
        super();

        setBuzz(bz);
    }

    @Override
    public void onModuleLoad() {
        // ...etc.
    }
}

I would like to "inject" FizzModule with a Buzz instance. However, all of the code examples I see for GWT modules do not use constructors. Instead, they bootstrap the DI mechanism (typically either ClientFactory or GIN) from inside the onModuleLoad() method. Is this something that GWT forces, or can I somehow inject my module before it loads to the client-side? Thanks in advance!

È stato utile?

Soluzione

GWT instantiates your module using its zero-arg constructor, always.

(technically, I think it uses GWT.create() so you could use deferred binding rules, but that wouldn't change anything re. how its instantiated)

BTW, where would the Buzz instance come from?

Altri suggerimenti

You could add parameters to the URL and use PlaceController. Then get those values on module load.

public void onModuleLoad() {
    SimplePanel mainPanel = new SimplePanel();
    EventBus eventBus = GWT.creat(EventBus.class);
    // Start ActivityManager for the main widget with ActivityMapper
    ActivityManager activityManager = new ActivityManager(injector.getActivityMapper(),
            eventBus);
    activityManager.setDisplay(mainPanel);
    RootPanel.get().add(mainPanel);

    // Start PlaceHistoryHandler with our PlaceHistoryMapper
    AppPlaceHistoryMapper contentHistoryMapper = GWT.create(AppPlaceHistoryMapper.class);
    PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(contentHistoryMapper);
    PlaceController placeController = new PlaceController(eventBus)
    historyHandler.register(placeController, injector.getEventBus(), new MainPlace());

    // Goes to the place represented on URL else default place
    historyHandler.handleCurrentHistory();
    if(placeController.getWhere() instanceof MainPlace) {
        (MainPlace).getFoo();
    }
}

public class MainPlace extends Place {

    private String foo;

    public MainPlace(String token) {
        String foo = token;
    }

    @Override
    public String getFoo() {
        return foo;
    }

    public static class Tokenizer implements PlaceTokenizer<MainPlace> {

        @Override
        public MainPlace getPlace(String token) {
            return new MainPlace(token);
        }

        @Override
        public String getToken(MainPlace place) {
            return place.getFoo();
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top