Sprint 2b – MVC – Controller

Controller Component Source Code

/* CONTROLLER object */
var gobjController = new function() {
  var self = this;
  var model;
  var view;

  self.taskSelect = function() {
    if (view.selectedItem(event)) {
      model.setSelectedTask(event.target.id);
    }
  };

  self.getTaskList = function() {
    return model.getTaskList();
  };

  self.setTaskIndex = function(pnumListIndex) {
    model.setListIndex( pnumListIndex);
  };

  self.editListItem = function() {
    if (!!model.getSelectedTask()) {
      view.showTaskDetails();
    }
  };

  self.addListItem = function() {
    view.setTaskText("");
    view.clearSelectedTask();
    model.setSelectedTask();
    view.showTaskDetails();
  };

  self.saveListItem = function() {
    var strTaskText = view.getTaskText();

    if (!!strTaskText) {
      if (!!model.getSelectedTask()) {
        model.updateTask(strTaskText);
        view.updateTask(strTaskText);
      }
      else {
        view.createTask(strTaskText, model.createTask(strTaskText));
      }
      model.setSelectedTask();
      view.showButtonPanel();
    }
  };

  self.deleteListItem = function() {
    if (!!model.getSelectedTask() &&
      confirm("Are you sure you want to remove this item?")) {
      model.deleteTask();
      view.deleteTask();
      view.showButtonPanel();
    }
  };

  self.cancelListItemChange = function() {
    view.showButtonPanel();
  };

  self.initialise = function(pobjModel, pobjView) {
    model = pobjModel;
    view = pobjView;

    pobjModel.initialise(self);
    pobjView.initialise(self);
  };
};

Return to previous page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s