First, I know there are lot of answers about MVC but I need some more-specific ansewer based on my probably wrong understanding of MVC.
I've already read this very good answer (Explain Model View Controller) but I still need some clarification.
I need to understand if I've understood how MVC works because I'm using it in a kind of MVC framework I'm developing.
- I have the views that are at the moment just "stupid" templates
- The models that are "things" that fetch data from database (or other sources) and transform them in arrays passed to the views.
- The controllers that are mostly ajax javascript scripts and observers for buttons and other user events and PHP scripts that are called by the Ajax scripts when I need to perform server-side operations like SQL updates, inserts etc.
So my "MVC understanding" makes my app works in this way:
I open my index that loads the viewsloader(model), this one loads the contents calling the contentsloader(model) that calls some other script depending by the view the user is requesting.
Then when all the data is loaded in a single array, the viewsloader(model) uses it to render the requested view and passes it to the user.
Now the user can click some button (for example a "view contacts" button) and under the hood my contactslistener(controller) calls the contactsloader(model) to load the contacts list, the array provided by the contactsloader(model) passed trhough the contactslistener(controller) which injects it in the view.
I think I have serious problems with the MVC model, looks like the controllers are doing too different things. Probably I should put the observers/listeners in the views part and use controllers just to load contents provided by the models.
- Are the models dedicated just to data fetching and organization (for example, fetch from database and put it in array organizing it with the right structure)?
- Are the controllers thought for handle user events or should I use views for this purpose? Should I use them to query the models and inject the answers into the views?
- Are the views just stupid templates or should I put there some business logic like event listeners? Following what I've read I should put the entire interface-side scripts here and put only (client/server)-side scripts in controllers. But in this case I've not idea about what a controller should do...