johnny-dreamguns
10/7/2019 - 9:38 AM

Facade

// FACADE

const task = function(data) {
  this.name = data.name;
  this.priority = data.priority;
  this.completed = false;
  this.completedDate = null;
}

const TaskService = function() {
  return {
    complete: function(task){
      task.completed = true;
    },
    setCompletedDate: function(task){
      task.completedDate = '01 Jan 2019';
    }
  }
}();

const myTask = new task({
  name: 'Test',
  priority: 2
});

// one way to do it
TaskService.complete(myTask);
if(myTask.completed){ 
  TaskService.setCompletedDate(myTask);
}

console.log(myTask);


const TaskServiceWrapper = function() {
  const setCompletedAndCompletedDate = function(task) {
    TaskService.complete(task);
    if(myTask.completed){ 
      TaskService.setCompletedDate(task);
    }
  }

  return {
    setCompletedAndCompletedDate
  }
}();

const myTask2 = new task({
  name: 'Test 2',
  priority: 3
});

// neater way to do it using a facade to tidy things up
TaskServiceWrapper.setCompletedAndCompletedDate(myTask2);

console.log(myTask);