4.6 Expanding Models - mmig/mmir GitHub Wiki

Expanding Models

Add a new Model

Now that we have the calendar controller and the required view, it is time to add the calendar model. To add a new model into your application, you have to navigate to www/models and add a new JavaScript file which has the same name as your model (in lowercase first letter). We name the new model calendarModel.js which should contain the following code:

var mmir = window.mmir ||
{};

mmir.CalendarModel = (function(){
  var instance = null;
  
  function constructor(){
    
    var calendar_server_url = 'http//...';
    
    return {
    
      save_appointment: function(data, cb_func){
        //We assume that the appointment should be created at a 
        //server running on 'THE-CALENDAR-SERVER-URL' and send   
        //the information via POST request to that server.
        var user_name = mmir.model.get('User').getInstance().getName();
        
        var calendar_server_url = 'calendar_server_url';//mmir.conf.get('calendar_server_url');
        
        var options = {
          type: 'POST',
          url: calendar_server_url + '?user_name=' + user_name,
          data: data,
          success: cb_func
        };
        
        //MOCK AJAX function:
        var send = function(options){
          console.info('MOCK AJAX request with options: '+JSON.stringify(options));
          if(options.success){
            options.success();
          }
        };
        
        //MOCK AJAX request:
        send(options);
        
//      $.ajax(options);
      }
      
    };
  }
  
  return {
    getInstance: function(){
      if (instance === null) {
        instance = constructor();
      }
      return instance;
    }
  };

})();

After creating the new model, we – again – need to update the file directories.json in assets/www/config/ by invoking the default Ant task generateFileListJSONFile in /build.xml (cf. section Add views of a new controller).


< previous: "Expanding Views" | next: "Expand Application Dictionaries" >