3.6. Project Structure # Application Controllers - dfki-flpe/sandbox-test GitHub Wiki
www/controllers
)
Application Controllers (This folder contains the controllers of your application. Controllers are one of the most important components of each MMIR application. In MSK you will find the file application.js
which contains the following code that specifies the controller Application
:
var mobileDS = window.mobileDS || {};
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();
mobileDS.User.create(email);
setTimeout(function(){
mobileDS.DialogEngine.getInstance().raise("user_logged_in");
},0);
};
Application.prototype.register = function(){
var email = $('#registration-form #email').val();
var password = $('#registration-form #password').val();
mobileDS.User.create(email);
setTimeout(function(){
mobileDS.DialogEngine.getInstance().raise("user_logged_in");
},0);
};
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 DialogEngine
, 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 DialogEngine
(see the example below and the API documentation and for ControllerManager
and DialogEngine
).
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:
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:
var data = $.parseJSON('{"user_name":"MAX"}');
mobileDS.ControllerManager.getInstance().performAction(
'Application', 'remove_user', data
)}
Currently, the argument for the name of controller should follow the guidelines described in section Naming Conventions for Models, Views, and Controllers (p. 10).