diff --git a/app/app.js b/app/app.js index fd7fb9a..9d6ac4c 100644 --- a/app/app.js +++ b/app/app.js @@ -1,84 +1,81 @@ -(function(){ +(function () { + var $ = document.getElementById.bind(document); + var $$ = document.querySelectorAll.bind(document); -var $ = document.getElementById.bind(document); -var $$ = document.querySelectorAll.bind(document); + var App = function ($el) { + this.$el = $el; + this.load(); -var App = function($el){ - this.$el = $el; - this.load(); + this.$el.addEventListener("submit", this.submit.bind(this)); - this.$el.addEventListener( - 'submit', this.submit.bind(this) - ); + if (this.dob) { + this.renderAgeLoop(); + } else { + this.renderChoose(); + } + }; - if (this.dob) { - this.renderAgeLoop(); - } else { - this.renderChoose(); - } -}; - -App.fn = App.prototype; - -App.fn.load = function(){ - var value; - - if (value = localStorage.dob) - this.dob = new Date(parseInt(value)); -}; - -App.fn.save = function(){ - if (this.dob) - localStorage.dob = this.dob.getTime(); -}; - -App.fn.submit = function(e){ - e.preventDefault(); - - var input = this.$$('input')[0]; - if ( !input.valueAsDate ) return; + App.fn = App.prototype; - this.dob = input.valueAsDate; - this.save(); - this.renderAgeLoop(); -}; + App.fn.load = function () { + var value; -App.fn.renderChoose = function(){ - this.html(this.view('dob')()); -}; + if ((value = localStorage.dob)) this.dob = new Date(parseInt(value)); + }; -App.fn.renderAgeLoop = function(){ - this.interval = setInterval(this.renderAge.bind(this), 100); -}; + App.fn.save = function () { + if (this.dob) localStorage.dob = this.dob.getTime(); + }; -App.fn.renderAge = function(){ - var now = new Date - var duration = now - this.dob; - var years = duration / 31556900000; + App.fn.submit = function (e) { + e.preventDefault(); - var majorMinor = years.toFixed(9).toString().split('.'); - - requestAnimationFrame(function(){ - this.html(this.view('age')({ - year: majorMinor[0], - milliseconds: majorMinor[1] - })); - }.bind(this)); -}; - -App.fn.$$ = function(sel){ - return this.$el.querySelectorAll(sel); -}; - -App.fn.html = function(html){ - this.$el.innerHTML = html; -}; - -App.fn.view = function(name){ - var $el = $(name + '-template'); - return Handlebars.compile($el.innerHTML); -}; - -window.app = new App($('app')) + var input = this.$$("input")[0]; + if (!input.valueAsDate) return; + this.dob = input.valueAsDate; + this.save(); + this.renderAgeLoop(); + }; + + App.fn.renderChoose = function () { + this.html(this.view("dob")()); + }; + + App.fn.renderAgeLoop = function () { + this.interval = setInterval(this.renderAge.bind(this), 100); + }; + + App.fn.renderAge = function () { + var now = new Date(); + var duration = now - this.dob; + var years = duration / 31556900000; + + var majorMinor = years.toFixed(9).toString().split("."); + + requestAnimationFrame( + function () { + this.html( + this.view("age")({ + year: majorMinor[0], + milliseconds: majorMinor[1], + }), + ); + }.bind(this), + ); + }; + + App.fn.$$ = function (sel) { + return this.$el.querySelectorAll(sel); + }; + + App.fn.html = function (html) { + this.$el.innerHTML = html; + }; + + App.fn.view = function (name) { + return Handlebars.templates[name]; + }; + + window.app = new App($("app")); })(); diff --git a/app/templates.js b/app/templates.js new file mode 100644 index 0000000..b55dff1 --- /dev/null +++ b/app/templates.js @@ -0,0 +1,77 @@ +(function () { + var template = Handlebars.template, + templates = (Handlebars.templates = Handlebars.templates || {}); + templates["age"] = template({ + compiler: [8, ">= 4.3.0"], + main: function (container, depth0, helpers, partials, data) { + var helper, + alias1 = depth0 != null ? depth0 : container.nullContext || {}, + alias2 = container.hooks.helperMissing, + alias3 = "function", + alias4 = container.escapeExpression, + lookupProperty = + container.lookupProperty || + function (parent, propertyName) { + if (Object.prototype.hasOwnProperty.call(parent, propertyName)) { + return parent[propertyName]; + } + return undefined; + }; + + return ( + '