Skip to content
Browse files

using localStorage to manage times

  • Loading branch information...
1 parent 3ae640d commit 11dd7fac5bec345afd83161232f8c28b356f28cb @aaronpowell committed Feb 24, 2011
Showing with 26 additions and 14 deletions.
  1. +19 −5 public/scripts/app.js
  2. +4 −8 public/scripts/chart.js
  3. +3 −1 views/layout.haml
View
24 public/scripts/app.js
@@ -1,16 +1,25 @@
doinnothin = (function(sw, $) {
-
- var times = [];
+
+ var bootup = new Date();
+ var dbId = bootup.getFullYear() + '-' + (bootup.getMonth()+1) + '-' + bootup.getDate();
+ var db = storageBox.getItem(dbId);
+ if(!db) {
+ db = {times:[]};
+ storageBox.setItem(dbId, db);
+ }
+ eventManager.trigger('time-refresh', this, { times: db.times });
var btn = $('#go').click(function(e) {
e.preventDefault();
if(sw.running()) {
sw.stop();
var stats = sw.stats();
- times.push(stats);
+ var db = storageBox.getItem(dbId);
+ db.times.push(stats);
+ storageBox.setItem(dbId, db);
btn.attr('value', 'I\'m doin\' nothin\'');
$('#time').text('');
- eventManager.trigger('timer-stopped', e, {stats: stats});
+ eventManager.trigger('time-refresh', e, {stats: stats, times: db.times});
} else {
sw.reset();
sw.start();
@@ -35,6 +44,7 @@ doinnothin = (function(sw, $) {
$('#save a').click(function(e) {
e.preventDefault();
+ var times = storageBox.getItem(dbId).times;
if(times.length) {
$.post('/save', { start: times[0].started, times: times }, function(result) {
$('#save a').addClass('saved');
@@ -43,15 +53,19 @@ doinnothin = (function(sw, $) {
});
var ret = {
- times: times,
+ times: function() {
+ return storageBox.getItem(dbId).times;
+ },
totalSeconds: function() {
+ var times = storageBox.getItem(dbId).times;
return times.map(function(x) {
return x.seconds;
}).reduce(function(x, y) {
return x + (y || 0);
});
},
totalMilliseconds: function() {
+ var times = storageBox.getItem(dbId).times;
return times.map(function(x) {
return x.milliseconds;
}).reduce(function(x, y) {
View
12 public/scripts/chart.js
@@ -1,9 +1,5 @@
-doinnothin.chart = (function(Raphael, $) {
+window.chart = (function(Raphael, $) {
- function refresh() {
- show(doinnothin.times);
- };
-
function show(times) {
$('#chart').children().remove();
var r = Raphael('chart'),
@@ -24,12 +20,12 @@ doinnothin.chart = (function(Raphael, $) {
.hover(fin, fout);
};
- eventManager.bind('timer-stopped', function(sender, args) {
- refresh();
+ eventManager.bind('time-refresh', function(args) {
+ show(args.times);
});
return {
- refresh: refresh
+ show: show
};
})(Raphael, jQuery)
View
4 views/layout.haml
@@ -61,8 +61,10 @@
%script{:src => '/scripts/modernizr-1.7.min.js'}
%script{:src => '/scripts/stopwatch.js'}
%script{:src => '/scripts/eventManager.js'}
- %script{:src => '/scripts/app.js'}
+ %script{:src => '/scripts/cookieMonster.js'}
+ %script{:src => '/scripts/storageBox.js'}
%script{:src => '/scripts/raphael-min.js'}
%script{:src => '/scripts/g.raphael-min.js'}
%script{:src => '/scripts/g.bar-min.js'}
%script{:src => '/scripts/chart.js'}
+ %script{:src => '/scripts/app.js'}

0 comments on commit 11dd7fa

Please sign in to comment.
Something went wrong with that request. Please try again.