Controller

Summary

The abstract base class for controllers.

Description

The Controller abstract class is the abstract base class for controllers in Altitude MVC/MVP applications.

Attaching views to models (via the observer pattern) and listening for user input events are done in individual methods to enable easy testing. In your application code, attach views and listen for events; in your test code, do not attach the views and do not listen for events.

Examples

Basic Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import Altitude.Frontend;
 
external $;
 
class CustomersController : Controller
{
    Views.Customers view = new Views.Customers();
    Models.Customers model = new Models.Customers();
 
    public override void attachViews() {
        this.model.attach(this.view);
    }
 
    public override void listenEvents() {
        $("#customers").click(function() {
            // ...
        });
    }
}
 
CustomersController customersController = new CustomersController();
customersController.attachViews();  // Do not do this in your tests
customersController.listenEvents(); // Do not do this in your tests
Decoupling Controller from the Web Browser / DOM Input Events
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Altitude.Frontend;
import Altitude.Frontend.ViewEngines;
 
class CustomersController : Controller
{
    IViewEngine viewEngine;
    Views.Customers view = new Views.Customers();
    Models.Customers model = new Models.Customers();
 
    // Use dependency injection to allow applications to specify the
    // concrete implementation of `IViewEngine` to use (such as
    // `BrowserViewEngine` for web browsers).
    public CustomersController(IViewEngine viewEngine) {
        this.viewEngine = viewEngine;
    }
 
    public override void attachViews() {
        this.model.attach(this.view);
    }
 
    public override void listenEvents() {
        viewEngine.get("customers").on("click", function() {
            // ...
        });
    }
}

Methods

  • attachViews

    The method to override to isolate code for attaching views to models.

  • listenEvents

    The method to override to attach event listeners.

Share

HTML | BBCode | Direct Link