nt.Views.Tracker = Backbone.View.extend({
el: '#tracker',
trackedTemplate: Handlebars.Templates.tracked,
emptyMessage: '<p>No foods are being tracked.</p><p>Do a search and add something!</p>',
emptyDate: '<p>There are no foods being tracked on this date.</p><p>Select another date or add a food.</p>',
events: {
'click #dtBack': 'dateBack',
'click #dtForward': 'dateForward',
'click #dtDisplay': 'dateDisplay',
'click .options a': 'setOption',
'click .tracked-delete': 'deleteFood',
'click .tracked-edit': 'openFood',
},
initialize: function() {
_.bindAll(this, 'sumCals', 'sumFat', 'sumCarbs', 'sumProt');
Handlebars.registerHelper({
title: this.title,
sumCals: this.sumCals,
sumFat: this.sumFat,
sumCarbs: this.sumCarbs,
sumProt: this.sumProt,
show: this.show
});
this.$trackerResults = $('#tracker-results');
this.$dtp = $('#dtPicker');
this.listenTo(this.collection, 'update', this.render);
this.duration = moment.duration({'days' : 1});
this.initDatePicker();
this.collection.fetch();
},
title: function(date) {
if(nt.Option.displayAll) return new Handlebars.SafeString('title="' + date + '"');
},
sumCals: function() {
return this.collection.calculateSum('valueCalories');
},
sumFat: function() {
return this.collection.calculateSum('valueTotalFat');
},
sumCarbs: function() {
return this.collection.calculateSum('valueTotalCarb');
},
sumProt: function() {
return this.collection.calculateSum('valueProteins');
},
show: function(attributeValue) {
var servings = this.attributes.servingCount;
var showValue = 0;
if(servings > 1) {
showValue = attributeValue * servings;