How to separate a view from a controller?

910 Views Asked by At

one of my co-workers reviewed my code in javascript and told me that I should not use a view in a controller. Instead, I should use a mediator. I'm totally lost. He will not be available until next week so here I am.

In my applications, I initialize a view in a controller (pseudo code) like below.

var controller = (function(){

   return {
      init: function()
      {
          this.view = new View("template");
          this.view.render();
      }
   }

})();

I have no idea how I separate a view from a controller and use mediator to work with them.

could anyone please give me an idea or a sample code or concept?

1

There are 1 best solutions below

7
On

First of all a bit on roles of Ms, Vs & Cs in a MVC pattern:

The three parts:

Model-View-Controller:

We will call the unchanging essence of the application/domain, the model (in the singular). In object-oriented terms, this will consist of the set of classes which model and support the underlying problem, and which therefore will tend to be stable and as long-lived as the problem itself.
How much should the model (classes) know about the connection to the outside world? Nothing, absolutely nothing.

Model-View-Controller:

For a given situation, in a given version there will be one or more interfaces with the model, which we'll call the views (plural). In object-oriented terms, these will consist of sets of classes which give us "windows" (very often actual windows).

Model-View-Controller:

A controller is an object that lets you manipulate a view. Over-simplifying a bit, the controller handles the input whilst the view handles the output. Controllers have the most knowledge of platforms and operating systems. Views are fairly independent of whether their event come from Microsoft Windows, X Windows or whatever.


What your co-worker is trying to tell you is this:

The below pic is a Sequence Flow(an approximate) pertaining to Zend framework, which is a MVC framework for PHP.

Click here for a bigger image.
Zend Framework calls


Observe the Dispatcher (3rd from last) in the above diagram.
Lets consider only the following for your case:

  • Front.php      : as your view.
  • Dispatcher    : the mediator your co-worker mentioned.
  • MyController : as your controller.

What you will have to do is this:
If the View has to trigger an event, it will not handle the event by itself. It will dispatch the event to the Dispatcher along with the parameters (if any) for the event.

The Dispatcher will now

  • Look for the controller that is capable of handling such an event.
  • Create the controller
  • Pass the event to the controller along with the parameters (if any).

The controller will now:

  • Prepare for the event.
  • Execute the event.
  • Return result-set(if any) to Dispatcher.

The Dispatcher will now:

  • Return the result-set(if any) back to the View.

The View will now:

  • Render the result-set(if any) and present it.

Why so much needs to be done?
To keep the roles segregated and clear.