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

Command

// COMMAND

const repo = {
  tasks: {},
  commands: [],
  get: function (id){
    console.log('Getting task');
    return {
      name: 'new task from db'
    }
  },
  save: function (task){
    repo.tasks[task.id] = task;
    console.log('Saving ' + task.name + ' to the db');
  }
}

repo.execute = function(name) {
  const args = Array.prototype.slice.call(arguments, 1);

  repo.commands.push({
    name,
    obj: args[0]
  }); 

  if(repo[name]){
    return repo[name].apply(repo, args);
  }
  return false;
}

repo.execute('save', {
  id: 1,
  name: 'Task 1',
  completed: false
});
repo.execute('save', {
  id: 2,
  name: 'Task 2',
  completed: false
});
repo.execute('save', {
  id: 3,
  name: 'Task 3',
  completed: false
});
repo.execute('save', {
  id: 4,
  name: 'Task 4',
  completed: true
});

console.log(repo.tasks);