Announcement

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

    SelectItem.addChangeHandler and getSelectedRecord()

    SmartGWT Pro 2.3 Nightly build 2010-11-22
    GWT 2.0.3
    All browsers


    I had a question around how SelectItem.getSelectedRecord() is supposed to work in combination with a ChangeHandler.The documentation on FormItem.addChangeHandler indicates that this handler is supposed to fire when an formItem is about to be changed but before the change has taken effect.

    However, when adding a ChangeHandler to a SelectItem and then getting the selectedRecord inside the onChange method of that handler; I'm getting the record representing the new value in the SelectItem, not the old one.
    The ChangeEvent itself does correctly return the old value in the getOldValue() method.

    Code:
    SelectItem assignedTo = new SelectItem(UserField.FULL_NAME.getName(),UserField.FULL_NAME.getDisplayName());
    
    
    	/* Binding to handle changes to assignedTo, prior to the change taking effect */
    			HandlerRegistration assignedToPreChangeReg = assignedTo.addChangeHandler(new ChangeHandler(){
    
    				public void onChange(ChangeEvent event) {
    					Log.debug("Pre change; old value: " + event.getOldValue() + "; new value: " + event.getValue());
    					Record oldAssignedTo = assignedTo.getSelectedRecord();
    					if(oldAssignedTo!= null && oldAssignedTo.getAttribute(UserField.EMAIL.getName())!= null)
    					{
    						String email = oldAssignedTo.getAttribute(UserField.EMAIL.getName());
    						Log.debug(this.getClass().getName() + " removing email address " + email +" from emailSelector");
    						emailSelector.removeEmail(email);
    					}
    				}
    				
    				
    			});
    I was actually expecting to get the old record.

    The functional requirement here is to add an email address to a recipient list when an order is assigned to a user. If the user is changed (from John Doe to Jane Doe, for instance); the previous added email address needs to be removed and the new one added.

    I can work around this by doing getting the old user id from ChangeEvent.getOldValue() and then calling the datasource for that record; but it's an extra round-trip to the server...

    #2
    Incidentally, assignedTo.getValue() also returns the old value; so it's kind of weird... SelectItem.getValue() returns the old value, but SelectItem.getSelectedRecord() returns the new value...

    Comment


      #3
      When the change() event fires, every API related to the FormItem's current value will return the old value (because you can cancel the change). But it's true that getSelectedRecord() does the lookup based on the new value. If you need the prior selected record for something, you'll need to cache it separately.

      Comment


        #4
        I'm a bit confused though, Isomorphic. I understand what you're saying, but what I'm finding is that the SelectItem and ComboBoxItem behave in opposite ways, as far as the addChangeHandler() is concerned:

        Code:
        SelectItem assignedTo  = new SelectItem(UserField.FULL_NAME.getName(),UserField.FULL_NAME.getDisplayName());
        
        assignedTo.addChangeHandler(new ChangeHandler()
        {
        
        public void onChange(ChangeEvent event) 
        	{
                           Log.debug(assignedTo.getSelectedRecord().getAttribute(UserField.FULL_NAME.getName())); 
                /* Returns the NEW selected record */
                }
        });
        versus

        Code:
        ComboBoxItem assignedTo  = new ComboBoxItem(UserField.FULL_NAME.getName(),UserField.FULL_NAME.getDisplayName());
        
        assignedTo.addChangeHandler(new ChangeHandler()
        {
        
        public void onChange(ChangeEvent event) 
        	{
                           Log.debug(assignedTo.getSelectedRecord().getAttribute(UserField.FULL_NAME.getName())); 
                /* Returns the OLD selected record */
                }
        });
        That can't be intended - they should behave the same way, no?

        Comment

        Working...
        X