3.6 Application Controllers - mmig/mmir GitHub Wiki

Application Controllers (www/controllers)

This folder contains the controllers of your application. Controllers are one of the most important components of each MMIR application. In the StarterKit example (see also section 4.4) you will find the file application.js which contains the following code that specifies the controller Application:

attention
  var mmir = window.mmir || {};
  
  var Application = function(){
      this.name = "Application";
  };

  Application.prototype.on_page_load = function(ctrl, data){
	  
  };
  
  Application.prototype.login = function(){
  
      var email = $('#emailField #email').val();
      var password = $('#passwordField #password').val();
      
      mmir.model.get('User').create(email);
      
      mmir.dialog.raise("user_logged_in");
  };

  Application.prototype.register = function(){
  
      var email = $('#registration-form #email').val();
      var password = $('#registration-form #password').val();
      
      mmir.model.get('User').create(email);
      
      mmir.dialog.raise("user_logged_in");
  };

Figure 5: Initial code of application controller

The on_page_load function will be called after rendering each view of the controller Application; this method must be implemented by each controller. This function definition also shows the default arguments for functions in controller implementation: if a function is called via the DialogManager, these 2 arguments are always supplied. The first argument is ctrl, which is a reference to the Controller class instance, and the second argument is data, which is passed on from the call to DialogManager (see the example below and the API documentation and for ControllerManager and DialogManager).

Optionally, you can provide a function on_page_load_<view name> for each view (e.g. on_page_load_welcome for the view welcome.ehtml); this function is called directly after the on_page_load function when the corresponding view is rendered, that is if the function exists.

As you can see, there are 2 more functions login and register, which you might not require in your application. These demonstrate the process of registering a new user and logging in an existing user. If you like, you can remove these methods from application controller or adjust them to your application logic. Note, as these functions do not utilize the arguments ctrl or data, these are simply omitted.

You also can add new methods to the Application controller. For example, you can add a function for removing an existing user from your database as follows:

attention
 Application.prototype.remove_user = function(ctrl, data){
    var user_name = data.user_name;
    //write your code here
 }

data is a well-formatted JSON object which contains the information (parameters) which you would like to send to the method (in this case the user_name).To call this method from other controllers or scripts in your application you need to first define the data object and call the method through ControllerManager as follows:

attention
 var data = $.parseJSON('{"user_name":"MAX"}');
 mmir.ctrl.perform(
  'Application', 'remove_user', data
 )};

The argument for the name of controller should follow the guidelines described in section Naming Conventions for Models, Views, and Controllers (i.e. start with an upper case letter).


< previous: "Application Resource Files" | next: "Application Helpers" >

⚠️ **GitHub.com Fallback** ⚠️