todos.js 870 B

12345678910111213141516171819202122232425262728293031
  1. $(function () {
  2. // Create a model class.
  3. const TodoItem = Backbone.Model.extend({
  4. // RESTful web service, RoR flavor.
  5. urlRoot: '/server/todos'
  6. });
  7. // Create a model instance to load it by ID.
  8. const todoItem = new TodoItem({ id: 1 });
  9. // Access the server at /server/todos/1 (urlRoot + '/' + item.id).
  10. const x = todoItem.fetch({ async: false });
  11. // Manipulate attributes.
  12. const description = todoItem.get('description');
  13. todoItem.set('status', 'complete');
  14. // Save on server.
  15. todoItem.save();
  16. // Create a view class.
  17. const TodoView = Backbone.View.extend({
  18. render: function () {
  19. const html = '<h3>' + this.model.get('description') + '</h3>';
  20. $(this.el).html(html);
  21. }
  22. });
  23. // Create a view instance.
  24. const todoView = new TodoView({ model: todoItem });
  25. todoView.render();
  26. $('#app').html(todoView.el);
  27. });