0

我有一个 GWT 应用程序,它在加载页面时加载产品。每当发生更改时,我都会在产品对象(及其子对象)上使用 PropertyChangeEvent 来更新字段的值。

当然,我不希望在第一次加载产品时引发此 PropertyChangeEvent。为此,我将 raisePropertyChange 值设置为 false,但它似乎不起作用。请在代码库下方找到:

// Class ProductBaseImpl
public abstract class PropChangeImpl {

    // The raise property change event, should be turned off conditionally
    private boolean raisePropertyChangeEvent = true;

    protected boolean getRaisePropertyChangeEvent() {
        return this.raisePropertyChangeEvent;
    }

    protected void setRaisePropertyChangeEvent(final boolean value) {
        this.raisePropertyChangeEvent = value;
    }

    protected void raisePropertyChangeEvent(String fieldName, Object oldValue, Object newValue) {
        if (this.raisePropertyChangeEvent ) {
            // --> HERE IS THE PROBLEM <--
            // This IF loop must not be true when loading the product first time
            System.out.println("Property change event raised!");
            // the update operations go here
        } else {
            System.out.println("Property change event not raised!");
        }
    }
}

// Class ProductBaseImpl
public abstract class ProductBaseImpl extends PropChangeImpl {

    private static HandlerRegistration productChangeBeginRegistration;
    private static HandlerRegistration productChangeEndRegistration;

    protected E instance;

    protected ProductBaseImpl(final E instance) {
        this.instance = instance;

        // Stop updates when a new product loads
        if (ProductBaseImpl.productChangeBeginRegistration == null) {
            ProductBaseImpl.productChangeBeginRegistration = Core.getEventBus().addHandler(ProductChangeBeginEvent.TYPE, new  ProductChangeBeginEventEventHandler() {
                @Override
                public void onProductChangeBegin(final ProductChangeBeginEvent event) {
                    ProductBaseImpl.this.raisePropertyChangeEvent(false);
                }
            });
        }

        if (ProductBaseImpl.productChangeEndRegistration == null) {
            ProductBaseImpl.productChangeEndRegistration = Core.getEventBus().addHandler(ProductChangeEndEvent.TYPE, new ProductChangeEndEventtHandler() {
                @Override
                public void onProductChangeEnd(final ProductChangeEndEvent event) {
                    ProductBaseImpl.this.raisePropertyChangeEvent(true);
                }
            });
        }
    }
}

// Class ProductSubObj1
public class ProductSubObj1 extends ProductBaseImpl {
    public ProductSubObj1 (final E instance) {
        super(instance);
        // some other operations
    }
}

// similar to above, I have classes ProductSubObj1, ProductSubObj2  ...


// Class ProductProvider, that fetches the product from service to UI
public class ProductProvider {

    // some properties and members

    public void fetchProduct(String productId) {

        // Let listeners know the product is about to change
        Core.getEventBus().fireEvent(new ProductChangeBeginEvent(productId));

        // Call the service to get the product in Json data

        // After processing the data to be available for the UI  (and scheduleDeferred)
        Core.getEventBus().fireEvent(new ProductChangeEndEvent(productId));
    }
}

正如代码中的内联注释,控件始终位于

if (this.raiseDataChangeEvent) 

第一次加载产品时我不想发生的块。

你能告诉我我做错了什么吗?

谢谢。

4

1 回答 1

0

你可以这样做吗:?

protected void raisePropertyChangeEvent(String fieldName, Object oldValue, Object newValue) {
        if (this.raisePropertyChangeEvent && oldValue != null /*Or whatever your default unloaded value is*/) {
            // --> HERE IS THE PROBLEM <--
            // This IF loop must not be true when loading the product first time
            System.out.println("Property change event raised!");
            // the update operations go here
        } else {
            System.out.println("Property change event not raised!");
        }
    }
于 2012-04-05T18:52:13.450 回答