Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to programmatically trigger FormItem change event

    I'm trying to implement a custom prompt feature. I've added a FormItemIcon to the field I want to prompt. When the icon is clicked I open a window where the user can select a value for the FormItem (a TextItem in this case). Once they make a selection I use FormItem.setValue() to put the selected value in the FormItem.

    But there is a ChangedHandler on the FormItem and it is not triggered by setValue(). It needs to be triggered in this case, just as if the user entered the selected value manually. How can I trigger the ChangedEvent on the FormItem?

    #2
    PS. I've tried the following, and while it does trigger the ChangedEvent, the event does not include the newly set value.

    promptedField.setValue(selectedValue);
    promptedField.fireEvent(new ChangedEvent(promptedField.getJsObj()));

    In the promptedField's changedHandler event.getValue() does not return the value set by setValue().

    Comment


      #3
      This is by design, because it really invites infinite loops (we call you from Changed() and then somewhere, indirectly, you manually triggered a ChangedEvent). We recommend instead just calling the relevant logic both from Changed and from wherever you programmatically setValue().

      Comment


        #4
        I have a somewhat similar issue. Instead of a FormItem, I'm using a SelectItem. When the form is instantiated to edit a record dfFoo.editNewRecord(data), I call setValue() on the SelectItem. Unfortunately this does not set the selectedRecord on that item, which I need to perform some calculation logic.

        I had thought that calling fireEvent would be the solution, but your comments warn against that.

        How can I set the selected record instead of just calling setValue()?

        Note that the SelectItem in question is driven by an optionDatasource and not the record passed to dfFoo.editNewRecord.
        Last edited by ls3674; 14 May 2012, 09:04.

        Comment


          #5
          It sounds like you need to do a setValue() and then 'fetchData()' in order to perform a fetch to retrieve the record associated with the new value.
          Once the fetch returns getSelectedRecord() should give you back the correct record.

          Let us know if that doesn't resolve this for you.

          Comment


            #6
            Isomorphic,

            I have a similar requirement where I have multiple DynamicForms all managed through a ValuesManager. Dynamic Forms have components which act on some events: Clicking a check box in the form would enable a Combobox etc. Now I'm planning to implement something like a state for a Form. User submits a form, then I need to save the form state i.e the values of formItems and enabled/disabled etc. Later I should be programatically re-set the state of the form. Change handlers are not fired when resetValues or setValue is fired on Valuesmanager or individual FormItem. What should be the approach for this.

            Thanks.

            Comment


              #7
              Once again: if you have logic that you want to run on Changed and in other circumstances, we recommend just calling the same logic in both circumstances.

              Comment


                #8
                The event system is the best about JavaScript to have lose coupling.

                I achieved it by overriding the changedEvent method getValue:

                ChangedEvent event = new ChangedEvent(this.getJsObj()){
                @Override
                public Object getValue() {
                return MyItemClass.this.getValue();
                }
                };

                fireEvent(event);

                Comment

                Working...
                X