Breaking event cycles in GUIs - user-interface

When writing GUIs, I've frequently come over the following problem: Assume you have a model and a controller. The controller has a widget W that is used to show a property X of the model.
Because the model might be changed from outside the controller (there might be other controllers using the same model, undo operations etc), the controller listens to changes on the model. The controller also listens to events on the widget W and updates the property X accordingly.
Now, the following happens:
the value in W is changed
an event is generated, the handler in the controller is invoked
the controller sets the new value for X in the model
the model emits events because it has been changed
the controller receives a change event from the model
the controller gets the value of X and sets it in the widget
goto 1.
There are several possible solutions for that:
Modify the controller to set a flag when the model is updated, and not react to any events from the model if this flag is set.
Disconnect the controller temporarily (or tell the model not to send any events for some time)
Freeze any updates from the widget
In the past, I usually went for option 1., because it's the simplest thing. It has the drawback of cluttering your classes with flags, but the other methods have their drawbacks, too.
Just for the record, I've had this problem with several GUI toolkits, including GTK+, Qt and SWT, so I think it's pretty toolkit-agnostic.
Any best practices? Or is the architecture I use simply wrong?
#Shy: That's a solution for some cases, but you still get a round of superfluous events if X is changed from outside the controller (for instance, when using the command pattern for undo/redo), because then the value has changed, W is updated and fires an event. In order to prevent another (useless) update to the model, the event generated by the widget has to be swallowed.
In other cases, the model might be more complex and a simple check on what exactly has changed might not be feasible, e.g. a complex tree view.

The standard QT way of dealing with this and also the one suggested in their very useful tutorial is to make the change to the value in the controller only if the new value is different from the current value.
This is way signals have the semantics of valueChanged()
see this tutorial

Usually you should respond to input events in the widget and not to change events. This prevents this type of loop from occuring.
User changes input in the widget
Widget emits change event (scroll done / enter clicked / mouse leave, etc.)
Controller responds, translates to change in the model
Model emits event
Controller responds, changes value in widget
Value change event emitted, but not listened to by controller

Flags to indicate updaing work. You can wrap them in methods like BeginUpdate and EndUpdate.

Related

How to add to an ObservableCollection from a different view?

(I'm using Prism Dryloc) My application consists of two views. The first contains a single list view displaying strings and the second - an entry and a button.
The listview is bound to an observable collection in the first page's View Model. How can I add to the observable collection from a different view?
Great question! You're essentially trying to pass data between views, and there's several ways of passing data between Views in Xamarin.Forms.
The two ways that seem relevant for your case:
Either making the ObservableCollection a public static object (so there's only one global instance of it). Not recommended.
The better way is to use the messaging center so that the second page publishes an event when the button is pressed, that the first page is subscribed to. And it passes that information that gets added to the list.
If these don't work, elaborate your use case and I'll suggest some more

extjs Save component view state before removing it from parent component

Let me explain the my scenario. I am using ExtJS5. I have got a view component (let us name is viewOne) contain two combo boxes, button search and some action button, on click of search button the grid is populated. This viewOne is in a parent view component (viewParent). I need to load a second view (viewTwo) on selecting some grid row and clicking some action button a new view is loaded (viewTwo) in the parentView. When I come back from viewTwo to viewOne I need old values of combo boxes to re perform the search.
Currently I am storing the values of combo boxes in a store and set then when the after view render and call search. We have discarded card layout for this implementation.
I wanted to know how this can be done via Ext.state , I cannot find any example on the same that is close solution to my problem. Any other way of doing this ?
State seems reasonable for that. First of all you must set StateProvider (by calling Ext.state.Manager.setProvider) to instance of class which extends Ext.state.Provider.
Then state should work for you. Only thing that you must remember is to set stateful property to true and set stateId of the component.
Usually you don't need to save state by yourself. All built-in stateful components have defined state events. When such state event occur (eg. expand on panel) then state is saved automatically. You can customize state events by calling addStateEvents in initComponent.
To provide custom component state you should override applyState and getState methods. When you combine it with addStateEvents you have all what you need.
Example: http://jsfiddle.net/48jw81da/16/

MVC - who decides what to do if there was no content?

When loading a page that lists for example events from a DB, but there are no events going on right now, who's decision should it be to show a message "sorry no events going on right now..."?
Should the model check if the array with events is empty, and call the view that holds this message as static content
Or should the view check if the array with content is empty, and if so display a message that informs the user?
Or is this opinion based and it depends on personal preference?
If it is based on personal preference, what are some of the advantages and disadvantages of either method?
EDIT:
The same question could be applied to: We are trying to load an event, but the event is over, so it will say "sorry, this event is already over". Again: Model's choice or View's choice to say this?
View is the part of triad, which will be aware that there is no content, when said view request the list of active events from model layer and gets back nothing.
If you are looking at this in the context of PHP, then your view won't be observing the model layer and will have to initiate the request for data.
Also, if you actually send back completely nothing, then the same view should also set the response code to 204.
When to show which kind of message is obviously a choice of the view. The model shoudln't know anything about users reading messages but only about its inner state.
The actual question is, how does the view know when to decide about which message should be shown. There are mainly two ways I could think of:
The view polls the state of the model each minute/second/millisecond/when-user-clicks-button/whatever and updates the messages if needed
or
The model itself emits events when its state changes and the view listens to those events.
But, the model emitting an event saying "my state is changed" is something totally different from deciding to show a message to the user and both should be strictly seperated.

Model-View-Controller: who notifies the View

The user-input is received by the Controller. The Controller manipulates/edits the Model.
The View can query the Model in order to obtain the new state for diplaying. The point I do not get is who actually notifies the View of changes? Because in the schematic overview, It seems that the Controller sends modification messages and that the Model also notifies the View of changes.
Even in the example, both notify the View. If the controller sends for example the position of the needle, why must the Model still notify the View?
[The figures are from the slides of our prof, so they are correct anyway]
It was becoming a bit confusing, so I googled to see what some leading authorities have to say, because Wikipedia and the first hits are not as good as they seemed to be.
Model. The Model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the View), and responds to instructions to change state (usually from the Controller).
View. The View manages the display of information.
Controller. The Controller interprets the mouse and keyboard inputs from the user, informing the Model and/or the View to change as appropriate.
So it seems that the Controller notifies the View.
But it is important to note that both the View and the Controller depend on the Model. However, the Model depends on neither the View nor the Controller. This is one of the key benefits of the separation. This separation allows the model to be built and tested independent of the visual presentation.
And of course the Controller cannot be the only one who changes the Model. For that reason there are indeed some variations:
The passive model is employed when one Controller manipulates the Model exclusively. The controller modifies the Model and then informs the View that the Model has changed and should be refreshed. The Model in this scenario is completely independent of the View and the Controller, which means that there is no means for the Model to report changes in its state.
The active model is used when the Model changes state without the controller's involvement. This can happen when other sources are changing the data and the changes must be reflected in the Views. Because only the Model detects changes to its internal state when they occur, the Model must notify the views to refresh the display.
-> Explains the case.
Model does not have to necessarily notify the View - in that case you get a passive implementation of MVC, see wikipedia.
The model can for example compute something in the background, e.g. it could be computing positions of particles in a particle system and here and there it could notify the view to update itself - i.e. the push model which is often more efficient than polling the model from the View.
For example before WebSockets and Comet, it would be always the Web view notifying the Controller which would poll a Model and render a new View. With WebSockets or Comet you can have the Controller notifying the View.
In any case, there are myriads of spins on implementing MVC, it's not set in stone and of course you can adapt it to your use case.

WP7 MVVMCross Detect RequestClose or BackKeyPressed inside ViewModels

I have two view. I navigate from one to the other and in the other I call RequestClose or use the back keypress. How can I detect in the first ViewModel these events?
Regards,
Dan
By default the MvvmCross framework doesn't tell you about this event.
You'll need to work out some other way of letting the ViewModel know that it needs to do something.
How you do this depends on what the actual event is that you are looking for.
For example:
if the second ViewModel changed some data in an underlying model, then this might be communicated back to the first ViewModel through an event from the data layer.
if the second ViewModel was somehow changing something more transitory (less model like) then you might implement some sort of messaging mechanism (using something like TinyMessenger) to allow ViewModels to communicate.
Can you say any more about what your event is?
Update... as an example, here's the type of thing I might do for facebook - https://github.com/slodge/facebookExample (WP7 only checked in!)

Resources