Add tasks for todo list

Ktb214 :

I am trying to add tasks for each todo list that has a specific title.

Can I get a specific todo list by its id and add some tasks to it? I am new to javascript, so I searched google about adding lists for a specific list with no results :(

class Model {
  constructor() {}

  this.todos = [
      { 
        id: 1,
       title: 'Outside',
       text: 'Running',
       complete: false,
       tasks: [
       { id: 1, text: 'Run a marathon', complete: false},
       { id: 2, text: 'Run with freinds', complete: false}
       ]
    },
    { 
        id: 2,
       title: 'Garden',
       text: 'Plant',
       complete: false,
       tasks: [
       { id: 1, text: 'Plant a garden', complete: false},
       { id: 2, text: 'Water the garden', complete: false}
       ]
    }];

    addTodo(todoText) {
        const todo = {
        id: this.todos.length > 0 ? this.todos[this.todos.length - 1].id + 1 : 1,
        text: todoText,
        complete: false,
        tasks: []
    }

    this.todos.push(todo)
  }
  }

Is it true to do like addTodo function for adding a tasks for a specific todo list like this?

addTodoTask(todoTaskText) {
        const todoTask = {
        id: this.todos.tasks.length > 0 ? this.todos[this.todos.tasks.length - 1].id + 1 : 1,
        text: todoText,
        complete: false,
    }

    this.todos.tasks.push(todoTask)
  }

and how to add a list of a list in javascript like:

<ul>
<li>Running
<ul>
<li>Run a marathon</li>
<li>Run with freind</li>
</ul>
</li>
</ul>
Mr. Polywhirl :

You could make each class handle rendering its own content and just map the list items consecutively while rendering from the top-down.

Edit: The render() methods make use of ES6 template literals. These are special strings that allow you embed variabes and expressions without the use of string concatenation.

const main = () => {
  let todoList = new TodoList({ todos : getData() })
  document.body.innerHTML = todoList.render()
}

class TodoTask {
  constructor(options) {
    this.id = options.id
    this.text = options.text
    this.complete = options.complete
  }
  render() {
    return `<li>[${this.id}] ${this.text} (${this.complete})</li>`
  }
}

class TodoEntry {
  constructor(options) {
    this.id = options.id
    this.title = options.title
    this.text = options.text
    this.complete = options.complete
    this.tasks = []
    if (options.tasks) {
      options.tasks.forEach(task => this.addTask(task))
    }
  }
  addTask(task) {
    this.tasks.push(new TodoTask(Object.assign({
      id : (this.tasks.length || 0) + 1
    }, task)))
  }
  render() {
    return `<li>
      [${this.id}] ${this.title} (${this.complete})
      <ul>${this.tasks.map(task => task.render()).join('')}</ul>
    </li>`
  }
}

class TodoList {
  constructor(options) {
    this.todos = []
    if (options.todos) {
      options.todos.forEach(todo => this.addTodo(todo))
    }
  }
  addTodo(todo) {
    this.todos.push(new TodoEntry(Object.assign({
      id : (this.todos.length || 0) + 1
    }, todo)))
  }
  render() {
    return `<ul>${this.todos.map(todo => todo.render()).join('')}</ul>`
  }
}

function getData() {
  return [{
    id: 1,
    title: 'Outside',
    text: 'Running',
    complete: false,
    tasks: [{
      id: 1,
      text: 'Run a marathon',
      complete: false
    }, {
      id: 2,
      text: 'Run with freinds',
      complete: false
    }]
  }, {
    id: 2,
    title: 'Garden',
    text: 'Plant',
    complete: false,
    tasks: [{
      id: 1,
      text: 'Plant a garden',
      complete: false
    }, {
      id: 2,
      text: 'Water the garden',
      complete: false
    }]
  }]
}

main() // entry

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220820&siteId=1