9 Replies Latest reply on Apr 30, 2013 11:24 AM by chaluwa

    Binding of ListBoxes with Errai

    martin.haft

      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
          edewit

          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

            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
              edewit

              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

              1 of 1 people found this helpful
              • 4. Re: Binding of ListBoxes with Errai
                martin.haft

                Thanks, without Errai injection this works quite well .

                 

                Martin

                • 5. Re: Binding of ListBoxes with Errai
                  jmbarone

                  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
                    jfuerth

                    [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
                      chaluwa

                      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
                        jfuerth

                        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
                          chaluwa

                          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.