ZK @NotifyChange comment

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_40646143/article/details/86686182

grammar

@NotifyChange("anotherProperty")

@NotifyChange({"secondProperty","thirdProperty"})

@NotifyChange("*")

@NotifyChange(".")

description

  • In -> method (command or the setter method)
  • Use -> Notifications binder one or more attributes change
  • By default, the binding properties set by the setter method will be notified to change this property in the absence of this annotation situation. You can use this annotation to override the default notice on goal setter methods. You can also add annotations on the command method, has informed the command execution after property changes. To avoid default notification, please use @NotifyChangeDisabled in setter methods.
  • Given annotation elements "*", which means to inform all the properties view model.
  • Given annotation element. "" Examples of force is located at a position annotation reload class instance attributes instead.

 

example

 

public class OrderVM {

    //the order list
    List<Order> orders;

    //the selected order
    Order selected;

    //getter and setter


   
    @NotifyChange("selected")
    @Command
    public void saveOrder(){
        getService().save(selected);
    }


    /**在newOrder()方法中,我们更改属性“orders”和“selected”,
      *   因此,我们应用@NotifyChange来通知binder重新加载2个更改的属性命令执行之后。
      */
    @NotifyChange({"orders","selected"})
    @Command
    public void newOrder(){
        Order order = new Order();
        getOrders().add(order);
        selected = order;//select the new one
    }




    /** deleteOrder()也更改相同的2个属性。假设这个ViewModel类只有两个属性,
      *我们可以使用“*”(星号)来通知当前类中的所有属性。
      */
    @NotifyChange("*")
    @Command
    public void deleteOrder(){
        getService().delete(selected);//delete selected
        getOrders().remove(selected);
        selected = null; //clean the selected
    }
}

 

@NotifyChange(".")

  • Dot (.) And asterisk (*) a little bit different, because the asterisk @NotifyChange ( "*") just reload notify binder components bind them to bean properties, such as @bind (vm.person.firstname), @ bind (vm.person.lastname) and @bind (vm.person.fullname).
  • Bound to the component properties of the bean itself, that vm.person, will not be overloaded @NofityChange ( "*"). In this case, you must use @NotifyChange ( ".") Tell binder bean (entity classes) itself has changed bean bound to the component properties to be reloaded.
  • For what use cases, you will need it? In most cases, we will only bind a component property to a bean property. However, in some cases, you may want to bind the component property to calculate the results of multiple bean property, but you do not want to calculate the logic into the bean. Then the component properties can be bound to the whole bean, and a converter to convert data on a plurality of properties of the bean.

 

  1. If, travel websites have a page, customers must fill out and return travel holiday trip duration will automatically calculate and fill in user is displayed on the screen after any one field. Here, we use the converters duration calculation, rather than the logic implemented Trip class attribute. In this case, you need to comment on leaveDate and returnDate property setter @NotifyChange ( "."), In order to make changes or comments, whether leaveDate returnDate property at what time, duration properties can all be updated.

 

Examples official website

Page code

<hlayout>
Leave Date: <datebox value="@save(vm.trip.leaveDate)"/>
</hlayout>
<hlayout>
Return Date: <datebox value="@save(vm.trip.returnDate)"/>
</hlayout>
Duration including leave and return(day): 
<label value="@load(vm.trip) @converter(vm.durationConverter)" />
  • Because durationConverter need access to two properties (leaveDate, returnDate) so we will have to label the value of the trip is bound to an object, bean itself rather than individual properties of the bean.

@NotifyChange ( ".") Examples

public class DurationViewModel {
    private Trip trip = new Trip();

    public class Trip {
    private Date leaveDate;
    private Date returnDate;

    public Date getLeaveDate() {
        return leaveDate;
    }

    @NotifyChange(".")
    public void setLeaveDate(Date leaveDate) {
        this.leaveDate = leaveDate;
    }

    public Date getReturnDate() {
        return returnDate;    
    }

    @NotifyChange(".")
    public void setReturnDate(Date returnDate) {
        this.returnDate = returnDate;
    }
}


public Converter getDurationConverter(){
    return new Converter() {

public Object coerceToUi(Object val, Component component, BindContext ctx) {
    if (val instanceof Trip){
        Trip trip = (Trip) val;    
        Date leaveDate = trip.getLeaveDate();
        Date returnDate = trip.getReturnDate();

    if (null != leaveDate && null != returnDate){
        if (returnDate.compareTo(leaveDate)==0){
                return 1;
        }else if (returnDate.compareTo(leaveDate)>0){
            return ((returnDate.getTime() - leaveDate.getTime())/1000/60/60/24)+1;
            }
          return null;
        }
    }
    return null;
}
    public Object coerceToBean(Object val, Component component, BindContext ctx) {
        return null;
    }
        };
    }
}

 

Notify Programmatically -> (programmatically notice)

  • Sometimes we have to change the value of the property to be notified depend on run-time, so we can not determine the properties at design time command. In this case, we can use BindUtils.postNotifyChange (String queueName, String queueScope, Object bean, String property) dynamically change notification. The underlying mechanism of this notice is to subscribe to the event we discussed in the binder portion of the queue. It uses the desktop range event queue by default.

Examples of the following dynamic notifications


   String data;

// setter & getter


@Command
public void cmd() {
    if (data.equal("A")) {
    //其它代码
    BindUtils.postNotifyChange(null,null,this,"value1");
} else {
     //其它代码
    BindUtils.postNotifyChange(null,null,this,"value2");
    }
}
  • The first parameter postNotifyChange () is the name of the queue, the queue is the second parameter range. You can leave it blank to use the default queue name and scope.

 

Above with reference zk-mvvm-book.pdf

From google translation

 

 

Guess you like

Origin blog.csdn.net/qq_40646143/article/details/86686182
Recommended