todos.js 846 B

12345678910111213141516171819202122232425262728293031
  1. $(function () {
  2. // Create a model class.
  3. const TodoItem = Backbone.Model.extend({});
  4. // Create a model instance.
  5. const todoItem = new TodoItem({
  6. description: "Remember the milk",
  7. status: "incomplete",
  8. id: 1
  9. });
  10. // Manipulate attributes.
  11. const description = todoItem.get('description');
  12. todoItem.set('status', 'complete');
  13. // Access the server: needs some configuration.
  14. todoItem.url = '/server/todo';
  15. const x = todoItem.fetch({ async: false });
  16. // todoItem.save();
  17. // Create a view class.
  18. const TodoView = Backbone.View.extend({
  19. render: function () {
  20. const html = '<h3>' + this.model.get('description') + '</h3>';
  21. $(this.el).html(html);
  22. }
  23. });
  24. // Create a view instance.
  25. const todoView = new TodoView({ model: todoItem });
  26. todoView.render();
  27. $('#app').html(todoView.el);
  28. });