todoList.js 989 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const TodoList = Backbone.Collection.extend({
  2. model: TodoItem,
  3. url: '/server/todos',
  4. });
  5. $(function () {
  6. // Make a global instance
  7. todoList = new TodoList();
  8. // Get number of items
  9. console.log('List length', todoList.length);
  10. // Push item
  11. todoList.add(todoItem);
  12. console.log('List length after add', todoList.length);
  13. // Get item at index
  14. const id = todoList.at(0).get('id');
  15. // Get item by id.
  16. const item = todoList.get(id);
  17. console.log(`Item with id ${id} is the added item ?`, item === todoItem);
  18. todoList.remove(item);
  19. console.log('List length after remove', todoList.length);
  20. // todoList.reset([
  21. // { description: "Pick up milk", status: "incomplete" },
  22. // { description: "Do the dishes", status: "incomplete" },
  23. // { description: "Learn backbone", status: "incomplete" },
  24. // ]);
  25. //
  26. // console.log('List after reset', todoList.toJSON());
  27. todoList.fetch({ async: false });
  28. console.log('List after fetch', todoList.toJSON());
  29. });