From be221aa88d8d3aa2e5853f906c4b69cd281c6a4f Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 4 Jul 2014 13:47:16 +0200 Subject: [PATCH] use Function.bind() instead of passing the 'this' object to each and every function. but that's no nice solution either ... --- js/uhr.js | 191 +++++++++++++++++++++++++++--------------------------- 1 file changed, 94 insertions(+), 97 deletions(-) diff --git a/js/uhr.js b/js/uhr.js index 7e638a1..1fd1ece 100644 --- a/js/uhr.js +++ b/js/uhr.js @@ -52,26 +52,25 @@ // public interface methods (exported later) var start = function start() { - if (!isOn(this)) { - var uhr = this; + if (!isOn.bind(this)()) { this.timer = window.setInterval(function uhrTimer() { - uhr.options.time = new Date(); - update(uhr); - }, 1000); - update(this); - setCookie(this, 'uhr-status', 'on'); + this.options.time = new Date(); + update.bind(this)(); + }.bind(this), 1000); + update.bind(this)(); + setCookie.bind(this)('uhr-status', 'on'); } }; var stop = function stop() { - if (isOn(this)) { + if (isOn.bind(this)()) { window.clearInterval(this.timer); this.timer = null; - update(this); - setCookie(this, 'uhr-status', 'off'); + update.bind(this)(); + setCookie.bind(this)('uhr-status', 'off'); } }; var toggle = function toggle() { - if (isOn(this)) { + if (isOn.bind(this)()) { this.stop(); } else { this.start(); @@ -80,14 +79,13 @@ var setLanguage = function setLanugage(languageKey) { if (languageKey !== this.options.language) { this.options.language = languageKey; - var renderer = new UhrRenderer(language(this), this.element.find('.letterarea')); - var uhr = this; - renderer.render(this, function () { - uhr.currentMinute = -1; - update(uhr); - }); - setCookie(this, 'uhr-language', languageKey); - update(this); + var renderer = new UhrRenderer(language.bind(this)(), this.element.find('.letterarea')); + renderer.render.bind(this)(function () { + this.currentMinute = -1; + update.bind(this)(); + }.bind(this)); + setCookie.bind(this)('uhr-language', languageKey); + update.bind(this)(); } }; var setTheme = function setTheme(theme) { @@ -95,7 +93,7 @@ this.element.removeClass(this.options.theme).addClass(theme); $('#uhr-onoffswitch' + this.id).removeClass(this.options.theme).addClass(theme); this.options.theme = theme; - setCookie(this, 'uhr-theme', theme); + setCookie.bind(this)('uhr-theme', theme); } }; var setTime = function setTime(time) { @@ -108,7 +106,7 @@ } this.options.time = time; } - update(this); + update.bind(this)(); }; // private interface methods @@ -120,16 +118,16 @@ if (this.options.time === undefined) { this.options.time = new Date(); } - setupHTML(this); - wireFunctionality(this); + setupHTML.bind(this)(); + wireFunctionality.bind(this)(); if (userTime !== undefined) { this.time(userTime); } }; // private helper methods (not exported) // set up - var setupHTML = function setupHTML(uhr) { - var e = uhr.element; + var setupHTML = function setupHTML() { + var e = this.element; // Base clock area e.addClass('uhr'); e.empty(); @@ -139,17 +137,17 @@ e.append(''); e.append('
'); e.append('
'); - e.css('width', uhr.options.width); + e.css('width', this.options.width); var realWidth = e.width(); e.width(realWidth); e.height(realWidth); e.css('font-size', (realWidth / 40) + 'px'); - if (uhr.options.controls) { + if (this.options.controls) { // on/off switch - var toggleSwitch = $('
'); - toggleSwitch.append(''); - toggleSwitch.append('