9 Replies Latest reply: Apr 30, 2013 11:24 AM by Charles Odili RSS

Binding of ListBoxes with Errai

Martin Haft Newbie

Hi,

 

we are evaluating Errai with focus on Errai UIBinding and Errai DataBinding. For this we migrated part of an existing client to Errai (Errai 2.2.0.Final). Here we had problems to bind a ListBox to the model as ListBox does not implement HasValue (due to the code in BindableProxyAgent). So how to use <select/> and bind with errai or more general how to bind widgets that do not implement HasValue?

 

Martin

  • 1. Re: Binding of ListBoxes with Errai
    Erik Jan de Wit Novice

    Hi Martin,

     

    You can bind widgets that do not implement HasValue, but then they are readonly. If you want to create a <select/> why not use a ValueListBox.

     

    Hope that helps,

         Erik Jan

  • 2. Re: Binding of ListBoxes with Errai
    Martin Haft Newbie

    Hi Erik,

     

    ValueListBox implements HasValue and would be bound by Errai but it is not instantiated by Errai due to a non-trivial constructor. I.e. ValueListBox needs a Renderer<T> with T of type of the value. (Actually we tried an own extention of ValueListBox with trivial constructor that had its own generic Renderer. But than we ran into ClassCastExceptions.)

     

    Martin

  • 3. Re: Binding of ListBoxes with Errai
    Erik Jan de Wit Novice

    Hi Martin,

     

    Right, you can't let Errai @Inject the because of the constructor, but you can still bind it. I don't know why you get a class cast exception maybe because a ValueListBox is not a ListBox? Maybe you can post the full stack trace?

     

    Here is an example of how I use ValueListBox:

     

      @Bound
      @DataField("project")
      private ValueListBox projectListBox = new ValueListBox(new Renderer() {
        @Override
        public String render(Project project) {
          return project.getTitle();
        }
    
        @Override
        public void render(Project project, Appendable appendable) throws IOException {
          appendable.append(render(project));
        }
      });
    
    

    Hope this helps

     

         Erik Jan

  • 4. Re: Binding of ListBoxes with Errai
    Martin Haft Newbie

    Thanks, without Errai injection this works quite well .

     

    Martin

  • 5. Re: Binding of ListBoxes with Errai
    Jose Miguel Barone Newbie

    Hi ... i'm trying to do a <select/> binding to a ValueListBox.

    I follow Erik's advice but i have a ClassCastException when Errai set the value in the ValueListBox.

     

    java.lang.ClassCastException: java.lang.String cannot be cast to xxxxxx

     

    It seems that internally the binder try to do a setValue on the ValueListBox with a String type, but i correctly configured the generics part. :-(

     

    All the other fields are correctly binded.

    Thanks for any guide about how to resolve this issue.

  • 6. Re: Binding of ListBoxes with Errai
    Jonathan Fuerth Master

    [Edit: I had responded, but I see Jose started a new thread to deal with this new question. Response has been moved there]

  • 7. Re: Binding of ListBoxes with Errai
    Charles Odili Novice

    My errai project is modelled after the errai MVP demo which uses presenters and views. In such an approach, the ValueListBox will be defined in the view and probably accessed from the presenter, so can one do data-binding by annotating it like you just did? I usually do my binding within a @PostConstructed annotated method containing code such as:

    programme = dataBinder.bind(widget, "field").getModel() so my question is, how can ValueListBox be data-bound in such scenarios.

  • 8. Re: Binding of ListBoxes with Errai
    Jonathan Fuerth Master

    Hi Charles,

     

    I'm not an MVP expert, but by my understanding, in the MVP pattern, the view must not refer to any model classes. That would mean you're limited to simple types in your view: ValueListBox<String> and maybe ValueListBox<Integer>... but never ValueListBox<YourModelClass>.

     

    Because of this, if you choose to follow the MVP pattern, you'll need to create a Converter<YourModelClass, String> and use it when you bind your model property to the ValueListBox<String> widget.

     

    Hope that helps,

     

    Jonathan

  • 9. Re: Binding of ListBoxes with Errai
    Charles Odili Novice

    Well, I wonder how practical it will be to follow such a pattern to the letter. I actually have code like ValueListBox<MyModelClass> in my views, but that's all because I do not perform anything apart from rendering logic within the views. All logic for event handling, RPC e.t.c are done in the presenter. Ok, If I have to go with say ValueListBox<String> instead of ValueListBox<MyModelClass>, how can one guarantee that I can always convert/re-construct/represent MyModelClass from some string representation. Whats the community's experience with doing so.