Permalink
Browse files

handles bad data better

  • Loading branch information...
1 parent 3b5e8f9 commit 7337da8f42e19198d21161e0b48d7b61310ac65c @drewlesueur committed Dec 4, 2010
Showing with 20 additions and 11 deletions.
  1. +6 −0 Tip.coffee
  2. +6 −3 Tip.js
  3. +4 −4 View.coffee
  4. +4 −4 View.js
View
6 Tip.coffee
@@ -12,10 +12,16 @@ class Tip
setTax: (tax) ->
@tax = tax - 0
+ if _.isNaN(@tax)
+ @tax = 0
setCost: (cost) ->
@cost = cost - 0
+ if _.isNaN(@cost)
+ @cost = 0
setRate: (rate) ->
@rate = rate - 0
+ if _.isNaN(@rate)
+ @rate = 0
setNumberOfPeople: (numb) ->
if numb is ""
return false
View
9 Tip.js
@@ -10,13 +10,16 @@
Tip.prototype.guests = [];
Tip.prototype.updateTotals = function() {};
Tip.prototype.setTax = function(tax) {
- return (this.tax = tax - 0);
+ this.tax = tax - 0;
+ return _.isNaN(this.tax) ? (this.tax = 0) : null;
};
Tip.prototype.setCost = function(cost) {
- return (this.cost = cost - 0);
+ this.cost = cost - 0;
+ return _.isNaN(this.cost) ? (this.cost = 0) : null;
};
Tip.prototype.setRate = function(rate) {
- return (this.rate = rate - 0);
+ this.rate = rate - 0;
+ return _.isNaN(this.rate) ? (this.rate = 0) : null;
};
Tip.prototype.setNumberOfPeople = function(numb) {
var diff, i, old;
View
8 View.coffee
@@ -4,9 +4,9 @@ class View
constructor: (controller) ->
@controller = controller
render: (tip) ->
- $('#tax').val tip.tax
- $('#rate').val tip.rate
- $('#cost').val tip.cost
+ $('#tax').val tip.tax || ""
+ $('#rate').val tip.rate || ""
+ $('#cost').val tip.cost || ""
$('#tip').val tip.rate * .01 * (tip.cost + tip.tax)
$('#grand').val ($('#tip').val() - 0) + ($('#tax').val() - 0) + ($('#cost').val() - 0)
@@ -31,7 +31,7 @@ class View
<div class="person" id="person_#{id}">
<pre>
Name <input type="text" class="name" value="#{guest.name}" />
- Percent <input size="2" type="text" class="percent" value="#{percent}"/>%
+ Percent <input size="4" type="text" class="percent" value="#{percent}"/>%
cost w tax $<input type="text" readonly class="cost" value="#{cost}" />
tip $<input type="text" readonly class="tip" value="#{tippy}"/>
total $<input type="text" readonly class="total" value="#{total}"/>
View
8 View.js
@@ -6,9 +6,9 @@
};
View.prototype.initialRendered = false;
View.prototype.render = function(tip) {
- $('#tax').val(tip.tax);
- $('#rate').val(tip.rate);
- $('#cost').val(tip.cost);
+ $('#tax').val(tip.tax || "");
+ $('#rate').val(tip.rate || "");
+ $('#cost').val(tip.cost || "");
$('#tip').val(tip.rate * .01 * (tip.cost + tip.tax));
$('#grand').val(($('#tip').val() - 0) + ($('#tax').val() - 0) + ($('#cost').val() - 0));
return $('#numberOfPeople').val(tip.numberOfPeople);
@@ -37,7 +37,7 @@
tippy = (percent * .01) * cost;
total = cost + tippy;
guest = tip.guests[id];
- $('#guests').append($("<div class=\"person\" id=\"person_" + (id) + "\">\n <pre>\n Name <input type=\"text\" class=\"name\" value=\"" + (guest.name) + "\" />\n Percent <input size=\"2\" type=\"text\" class=\"percent\" value=\"" + (percent) + "\"/>%\n cost w tax $<input type=\"text\" readonly class=\"cost\" value=\"" + (cost) + "\" />\n tip $<input type=\"text\" readonly class=\"tip\" value=\"" + (tippy) + "\"/>\n total $<input type=\"text\" readonly class=\"total\" value=\"" + (total) + "\"/>\n </pre>\n</div>"));
+ $('#guests').append($("<div class=\"person\" id=\"person_" + (id) + "\">\n <pre>\n Name <input type=\"text\" class=\"name\" value=\"" + (guest.name) + "\" />\n Percent <input size=\"4\" type=\"text\" class=\"percent\" value=\"" + (percent) + "\"/>%\n cost w tax $<input type=\"text\" readonly class=\"cost\" value=\"" + (cost) + "\" />\n tip $<input type=\"text\" readonly class=\"tip\" value=\"" + (tippy) + "\"/>\n total $<input type=\"text\" readonly class=\"total\" value=\"" + (total) + "\"/>\n </pre>\n</div>"));
controller = this.controller;
return $("#person_" + (id)).find('input').each(function() {
return $(this).keyup(function(e) {

0 comments on commit 7337da8

Please sign in to comment.