PageRenderTime 101ms CodeModel.GetById 30ms RepoModel.GetById 9ms app.codeStats 0ms

/js/views/TodoStatsView.js

http://github.com/radekstepan/fundamental.js
JavaScript | 53 lines | 36 code | 8 blank | 9 comment | 0 complexity | c769105188c5107afe7c01048972b230 MD5 | raw file
  1. // Todo Statistics View
  2. // ----------
  3. App.Views.TodoStatsView = Backbone.View.extend({
  4. "el": "#todo-stats",
  5. // Our template for the line of statistics at the bottom of the app.
  6. // Compiles JavaScript templates into functions that can be evaluated for rendering.
  7. "template": _.template(function() {
  8. var result;
  9. // **Asynchronously** fetch the template from an external file when needed.
  10. $.ajax({
  11. "async": false,
  12. "url": "js/templates/_stats.html",
  13. success: function(data) {
  14. result = data;
  15. },
  16. });
  17. return result;
  18. }()),
  19. "events": {
  20. "click .todo-clear a": "clearCompletedTodos"
  21. },
  22. // Listen when stats have updated so we can re-render ourselves. We could also bind to the
  23. // Model itself.
  24. initialize: function(options) {
  25. _.bindAll(this, "render");
  26. App.Mediator.bind("todosStatsUpdated", this.render);
  27. },
  28. // Re-render us based on current **Todos Collection**.
  29. render: function(options) {
  30. $(this.el).html(this.template({
  31. "total": App.Models.Todos.length,
  32. "done": App.Models.Todos.done().length,
  33. "remaining": App.Models.Todos.remaining().length
  34. }));
  35. return this;
  36. },
  37. // Clear all done todo items, destroying their Models.
  38. clearCompletedTodos: function() {
  39. _.each(App.Models.Todos.done(), function (todo) {
  40. todo.destroy();
  41. });
  42. this.render();
  43. return false;
  44. }
  45. });