本文介绍了什么时候使用GWT ensureInjected()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CSSResource中创建了几种样式,无论我使用

  GWT。< MyResources> create MyResources.class).myStyles()。ensureInjected(); 



一个亮点,并解释何时使用ensureInjected或不?



谢谢!
Daniel

解决方案

正如Christian所说, ui.xml 文件,您不必担心调用 ensureInjected(),以下XML语句完成此任务:

 < ui:with field ='myStyle'type ='com ... MyStyle'/> 



 < div class = '{myStyle.redBorder}'/> 

当然,这里假设有一个 MyStyle interface defined:

  public interface MyStyle {
public String redBorder
}



现在我同意你的意见,当你需要操纵 CssResource 扩展外部的UiBinder模板。正确的是,在使用 MyStyle 实例之前,您必须在调用 ensureInjected() p>

我个人使用GIN注入 CssResource 的扩展实例,当我需要它们时。你可以实现一个自定义GIN提供程序确保在注入之前在 CssResource 上调用 ensureInjected()
有三个步骤:




  • 创建 MyStyle 以及CSS文件。

    MyStyle.java

      public interface MyStyle {
    public String redBorder();
    }


  • MyStyle.css

      .redBorder {border:1px solid red; } 


  • 通过 ClientBundle extension。

    Resources.java

      {
    @Source(MyStyle.css)
    public MyStyle style();
    }


  • 配置GIN提供程序方法注入 MyStyle

    ClientModule.java

      public class ClientModule extends AbstractGinModule {
    @Override
    protected void configure(){
    // ...
    }

    @ MyStyle createStyle(final Resources resources){
    MyStyle style = resources.style();
    style.ensureInjected();
    return style;
    }
    }




我们在上面顺利注入 Resources 实例,这意味着没有更多麻烦调用 GWT的静态访问器。< Resources> create



完成后,你可以注入你的实例 MyStyle

 <$ c 

$ c> private Widget widget;

@Inject
public SomeView(final MyStyle style){
// ...
widget = uiBinder.createAndBindUi(this);
widget.addStyleName(style.redBorder());
}


I created a few styles into a CSSResource and it works well whether I use

GWT.<MyResources>create(MyResources.class).myStyles().ensureInjected();

or not.

Could anyone shed a light on this and explain when to use ensureInjected or not?

Thank you!Daniel

解决方案

As Christian said, inside the UiBinder ui.xml file you don't have to worry about invoking ensureInjected(), the following XML statements do the job:

<ui:with field='myStyle' type='com...MyStyle'/>
<div class='{myStyle.redBorder}'/>

Of course this is assuming that there is somewhere a MyStyle interface defined:

public interface MyStyle {
    public String redBorder();
}

Now I agree with you that things get annoying when you need to manipulate the CssResource extension outside of UiBinder templates. Precisely because you have to take care of invoking ensureInjected() somewhere before using the MyStyle instance with your widgets.

I personally use GIN to inject instances of extensions of CssResource whenever I need them.That way you can implement a custom GIN provider ensuring that ensureInjected() is called on the CssResource before injecting it.There are three steps involved there:

  • Create an interface for MyStyle alongside with a CSS file.
    MyStyle.java

    public interface MyStyle {
        public String redBorder();
    }
    

  • MyStyle.css

    .redBorder { border: 1px solid red; }
    

  • Expose it through a ClientBundle extension.
    Resources.java

    public interface Resources extends ClientBundle {
        @Source("MyStyle.css")
        public MyStyle style();
    }
    

  • Configure a GIN provider method to inject your instances of MyStyle.
    ClientModule.java

    public class ClientModule extends AbstractGinModule {
        @Override
        protected void configure() {
        //...
        }
    
        @Provides MyStyle createStyle(final Resources resources) {
            MyStyle style = resources.style();
            style.ensureInjected();
            return style;
        }
    }
    

We smoothly inject the Resources instance here above, which means no more hassle of a static accessor calling GWT.<Resources>create(Resources.class) anywhere, it just all happens through the GIN injection.

Having done that you can inject your instances of MyStyle when you need them.
For example (in some MVP context):

private Widget widget;

@Inject
public SomeView(final MyStyle style) {
    //...
    widget = uiBinder.createAndBindUi(this);
    widget.addStyleName(style.redBorder());
}

这篇关于什么时候使用GWT ensureInjected()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 01:42