todoList.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const TodoList = Backbone.Collection.extend({
  2. model: TodoItem,
  3. url: '/server/todos',
  4. initialize() {
  5. this.on('remove', this.hideModel);
  6. },
  7. hideModel(model) {
  8. // "hide" is a custom event.
  9. model.trigger('hide');
  10. },
  11. });
  12. /* List events:
  13. - reset: on fetch() and reset()
  14. - add: when adding a model to the collection
  15. - remove: when removing a model from the collection
  16. Using:
  17. - todoList.on('reset', () => {}). Can be disabled by passing { silent: true }
  18. - todoList.off('reset', <same_callback>) to remove it
  19. - todoList.on('add' (or 'remove'), (modelItem) => { ... })
  20. Model events are also usable on collections. Triggered on the collection when
  21. they are triggered on a model within the collection:
  22. - change : attribute change
  23. - change:<attr> : specific attribute change
  24. - destroy : model destroyed
  25. - sync : model successfully synced
  26. - error
  27. - all
  28. */
  29. function todoListSingleExample(todoItem) {
  30. const todoList = new TodoList();
  31. // Get number of items
  32. console.log('List length', todoList.length);
  33. // Push item
  34. todoList.add(todoItem);
  35. console.log('List length after add', todoList.length);
  36. // Get item at index
  37. const id = todoList.at(0).get('id');
  38. // Get item by id.
  39. const item = todoList.get(id);
  40. console.log(`Item with id ${id} is the added item ?`, item === todoItem);
  41. todoList.remove(item);
  42. console.log('List length after remove', todoList.length);
  43. // todoList.reset([
  44. // { description: "Pick up milk", status: "incomplete" },
  45. // { description: "Do the dishes", status: "incomplete" },
  46. // { description: "Learn backbone", status: "incomplete" },
  47. // ]);
  48. //
  49. // console.log('List after reset', todoList.toJSON());
  50. todoList.fetch({ async: false });
  51. console.log('List after fetch', todoList.toJSON());
  52. return todoList;
  53. }
  54. function todoListFromServer() {
  55. const todoList = new TodoList();
  56. todoList.fetch({ async: false });
  57. // Collection forEach is /not/ Array.prototype.forEach.
  58. todoList.forEach(item => { console.log(item.get('description')); });
  59. // Not Array.prototype.map.
  60. const r1 = todoList.map(item => item.get('description'));
  61. console.log("r1", r1);
  62. // Returns an Array, not another Collection.
  63. const r2 = todoList.filter(item => item.get('status') === 'complete');
  64. console.log("r2", r2, r2.constructor.name);
  65. // And proxies to 46 Underscore methods.
  66. // http://backbonejs.org/#Collection-Underscore-Methods
  67. return todoList;
  68. }
  69. $(function () {
  70. // todoListSingleExample(globalTodoItem);
  71. // globalList = todoListFromServer();
  72. });