View Full Version : Where should take place an ajax request in mvc pattern: controller or view?

19 Jun 2012, 8:50 AM
I've created on my view a changepassword function that actually performs an ajax call to change the password of the selected user.

Now this is only a question about correct programming style, because I don't have any issue, however I'm interested in understanding how should I do this:

On the controller, I get the click event fired. At this point, I need to open a Ext.Msg.prompt to let the user insert the password and then run a function that basically run an Ext.Ajax call with correct parameters.

Currently, I did everything "view side" except click event detection, which simply fires the changePassword method on the view with the correct parameter (a record).

However I'm thinking about changing the code in this way:
changePassword method accepts a function as another parameter that is on the controller and will perform an ajax call. In this way, the ajax call will stay on the controller while the Msg.prompt on the view. Is this a better way to follow mvc pattern?

Tim Toady
19 Jun 2012, 11:14 AM
Why not do the prompt and define your changePassword function in the controller? You are already in the controller when you catch the event you are waiting for. A view should generally be just the structure of what you are seeing.

19 Jun 2012, 4:57 PM
If you are strictly following the prompt should happen on the view I think.
Anyway, thanks for your suggestion, is actually what I'm using because I feel more comfortable with it.