MVC pitfall - Model and View share the same reference

 
MVC framework has a very narrow goal.
That is to help you separate your application’s coding concerns into three discrete tiers; Model, View and Controller.
You should be aware, however, that letting the view and the model reference the same object means bypassing the MVC mechanism.
 
The results is that your proxy being updated without explicitly set it (usually in a command),
preventing aborting changes and reverting back to the original value.
 
The recommended flow for updating the model by the view is as follows:
The view notify regarding the change -> a command is being executed
In this case the flow of updating proxy from the view is:
View (change a value and notify change) -> Command(being executed) -> Proxy (updated by the command)
 
When the proxy and the view reference the same object you get:
View (change a value and notify change) ->Proxy (updated by the view)
                                                         ->Command(being executed) -> Proxy (updated again by the command)
 
This results in the proxy being updated immediately when the view is changed,
bypassing the MVC mechanism and preventing you from preventing the model from being updated by conditionally execute the command.
 
For example:
Consider a navigation tree, where each node represents a view with a "Save" button.
User made any action so the view was changed (became dirty).
Before saving, the user navigates away by selecting another node in the tree (view changes the selected node,
which is held by the model as well), and gets a "Do you want to save changes?" message.
In case the user cancels the navigation,  the selected node should revert to the original one.
 
In case both view and model reference the same selected node, the original value is lost by now.
 
The solution:
The view and the model should work on separate copies of the data.
 
Here is a link that also discusses the issue:

http://forums.puremvc.org/index.php?topic=1626.msg7347#msg7347

 

 

Front End Developer

Frontend Group
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com