简体   繁体   中英

Update the view although the model hasn't changed

My app updates the view in response to events dispatched by the model. But what if the model hasn't changed, but I still need to update the view. For example, I've closed and reopened a pop-up. The data to be displayed hasn't changed but the pop-up mediator and the view have to be recreated. My current solution is to force initialization in the mediator's onRegister() method like this:

// Inside of PopUpMediator.as

[Inject]
public var popUpModel:IPopUpModel;

[Inject]
public var popUpView:PopUpView;

override public function onRegister()
{
    // Force initialization if the model hasn't changed
    popUpView.foo = popUpModel.foo;

    // Event based initialization
    addContextListener(PopUpModelEvent_foo.CHANGE, foo_changeHandler);        
}

Injecting models into mediators isn't a good idea, so I'm wondering What is the best way to init the view when its model hasn't changed?

Well,

I supose you have View1 where you have popup button.

View2 is your popus.

so when View1 button is clicked, you dispatch an event from main mediator that goes to popupCommand where you add popup to contextView, or where you remove it.

You can also have one state inside a model, that will say popupVisible and when you change that property you dispatch a event that is listened in the main mediator and that adds or removes the popup. In that case command would alter the model property instead of adding popup directly to contextView.

Third way is to add popup manually inside the view, and since the stage is being listened to by robotlegs, popup will be mediated automatically.

I've decided to add an event called PopUpViewInitEvent . A command will check if the model was updated while the pop-up was closed. If not it will reinitialize the view by dispatching the PopUpViewInitEvent . The event will contain all the data required to initialize the view. This way I won't have to inject models into my mediator.

[Inject]
public var popUpView:PopUpView;

override public function onRegister()
{
    // Batch initialization
    addContextListener(PopUpViewInitEvent.INIT, batchInit);

    // Gradual initialization
    addContextListener(PopUpModelEvent_foo.CHANGE, foo_changeHandler);        
    addContextListener(PopUpModelEvent_bar.CHANGE, bar_changeHandler);        
}

protected function batchInit(event:PopUpViewInitEvent)
{
    popUpView.foo = event.foo;
    popUpView.bar = event.bar;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM