3 Replies Latest reply on Dec 12, 2013 11:38 AM by mbarkley

    Errai @ModelSetter Unsettles Data-Binding ?

    chaluwa

      I have a situation where using a @ModelSetter method in a @Templae @Page kinda turns off the binding between the @Model object and @Bound @Datafield widgets.

      Details here :

      @Inject @Model private PutmeApplication putmeApply;
        ....
      
      
        // observe a CDI event. Gets fired from the server.
        private void onJambRegValidation(@Observes PutmeApplication evt) {
             LogUtil.log("recieved CDI event");
             if (evt != null) {
                  LogUtil.log("record exists ...");
                  //importIntoModel(evt);
                  setModel(evt); // call method annotated with @ModelSetter
             }
        }
      
      
        @ModelSetter
        private void setModel(PutmeApplication model) {
             // this kinda "turn-off" the binding on putmeApply ?
             // Data in model parameter, which gets assigned to putmeApply do not show up in the UI
             // and data entered into bound UI data-fields to do show up in putmeApply fields
             this.putmeApply = model;
        }
      
      
        // bcos @ModelSetter private void setModel(PutmeApplication model) is "failing",
        // I had to do this for now ?
        private void importIntoModel(PutmeApplication src) {
             putmeApply.setFirstname(src.getFirstname());
             putmeApply.setLastname(src.getLastname());
             putmeApply.setDob(src.getDob());
             putmeApply.setGender(src.getGender());
             putmeApply.setJambScore(src.getJambScore());
             putmeApply.setFirstChoice(src.getFirstChoice());
             putmeApply.setJambreg(src.getJambreg());
        }
      
      
        • 1. Re: Errai @ModelSetter Unsettles Data-Binding ?
          mbarkley

          Hey Charles,

           

          Thanks for posting this. It looks like there's a bug with the @ModelSetter where the binding does not work when called from within the declaring class. I've filed a jira about it.

           

          In the mean time, you could use a DataBinder instead of the @Model declaration.

           

          Instead of:

          @Inject @Model ModelType model;
          

          You can do this:

          @Inject @AutoBound private DataBinder<ModelType> binder;
          

           

          Then to set and get the model:

          binder.setModel(model);
          // Gives back the bound model
          binder.getModel();
          

           

          Cheers.

          1 of 1 people found this helpful
          • 2. Re: Re: Errai @ModelSetter Unsettles Data-Binding ?
            chaluwa

            Ok. good. But is there still benefit to wrap binder.setModel(model); inside a @ModelSetter method ? E.g :

                 @ModelSetter
                 private void setModel(PutmeSubjectBundle model){
                   binder.setModel(model);
                 }
            
            • 3. Re: Re: Errai @ModelSetter Unsettles Data-Binding ?
              mbarkley

              No, you should get rid of the @ModelSetter annotation if you use a DataBinder. Calling setModel on the DataBinder is the only thing you need to replace the model.

              1 of 1 people found this helpful