fundamental.js /js/views/TodoStatsView.js

Language Javascript Lines 53
MD5 Hash c769105188c5107afe7c01048972b230
Repository git://github.com/radekstepan/fundamental.js.git View Raw File View Project SPDX
| Open JSFiddle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Todo Statistics View
// ----------

App.Views.TodoStatsView = Backbone.View.extend({

	"el": "#todo-stats",

	// Our template for the line of statistics at the bottom of the app.
	// Compiles JavaScript templates into functions that can be evaluated for rendering.
	"template": _.template(function() {
		var result;
		// **Asynchronously** fetch the template from an external file when needed.
		$.ajax({
			"async": false,
		    "url":   "js/templates/_stats.html",
		  	success: function(data) {
		    	result = data;
		  	},
		});
		return result;
	}()),

	"events": {
		"click .todo-clear a": "clearCompletedTodos"
	},

	// Listen when stats have updated so we can re-render ourselves. We could also bind to the
	// Model itself.
	initialize: function(options) {
		_.bindAll(this, "render");
		App.Mediator.bind("todosStatsUpdated", this.render);
	},

	// Re-render us based on current **Todos Collection**.
	render: function(options) {
		$(this.el).html(this.template({
			"total":      App.Models.Todos.length,
			"done":       App.Models.Todos.done().length,
			"remaining":  App.Models.Todos.remaining().length
		}));
		return this;
	},

	// Clear all done todo items, destroying their Models.
	clearCompletedTodos: function() {
		_.each(App.Models.Todos.done(), function (todo) {
			todo.destroy();
		});
		this.render();
		return false;
	}

});
Back to Top