1

here is my problem. I m using Gin in a gwt project, i m using GWT.create(SomeClass.class) to get instance ,but the problem is that i want signleton instance and for that purpose i bind that class in the app module to be singleton. Every tome i execute GWT.create(TemplatePanel.class) it returns different instance..why?? Here is snippet of my code. Module

public class AppClientModule extends AbstractGinModule
{
protected void configure()
{
  bind(MainPanel.class).in(Singleton.class);
  bind(TemplatePanel.class).in(Singleton.class);
}
} 

Injector

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
  MainPanel getMainForm();
  TemplatePanel getTemplateForm();
}

TemplatePanel

public class TemplatePanel extends VerticalPanel
@Inject
public TemplatePanel()
{
  this.add(initHeader());
  this.add(initContent());
}
..

MainPanel

public void onSuccess(List<MyUser> result)
    {
.......
  TemplatePanel temp = GWT.create(TemplatePanel.class);

.......
}

And the entry point

private final AppInjector injector = GWT.create(AppInjector.class);
public void onModuleLoad()
{
  MainPanel mf = injector.getMainForm();
  TemplatePanel template = injector.getTemplateForm();
  template.setContent(mf);
  RootPanel.get().add(template);
}
brakebg
  • 414
  • 3
  • 11
  • 24

2 Answers2

7

GWT.create(..) does not work with GIN, it just creates an object in a normal GWT way. You should either:

  1. Inject TemplatePanel in MainPanel, or

  2. Instantiate injector (via static method maybe) and then get TemplatePanel.

I usually have a static reference to injector (since you only need one per app) so I can access it anywhere:

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
    AppInjector INSTANCE = GWT.create(AppInjector.class);

    MainPanel getMainForm();
    TemplatePanel getTemplateForm();
}

(Note: constant interface fields are by definition public and static, so you can omit those.)

Then you'd use:

TemplatePanel temp = AppInjector.INSTANCE.getTemplateForm();
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Peter, i have got one more question, i see you are familiar with Gin. When i create an instance of any layout/widget for example HorizontalPanel, do you inject it or use new HorizontalPanel() ... and if you inject it ,how are you doing it? Thanks again. – brakebg Jun 02 '11 at 11:59
  • 1
    I always use UiBinder and don't have issues with creating/injecting widgets. But in rare occasions I use `new` for widgets. Injection is useful on a level where you might want to replace classes/objects with some other implementation (i.e. in a testing scenario). With widgets you usually don't need to do this, because you switch the whole View implementation. – Peter Knego Jun 02 '11 at 13:26
0

GWT.create simply calls new XXX where XXX is the class literal you passed to it. It does however does some magic when the XXX class has some rules in a module defined for it, aka Deferred Binding. I fyou can Gwt.create(YYY) and theres a rule that says if user agent is Internet Explorer 2 use ZZZ then it will.

mP.
  • 18,002
  • 10
  • 71
  • 105