From 60ceb1f39d316cec54b10716509d7d0e29b2ba10 Mon Sep 17 00:00:00 2001 From: Commander-lol <ljcapitanio@gmail.com> Date: Sun, 16 Nov 2014 17:44:32 +0000 Subject: [PATCH] Updated test and semi-linted tinyDOM.js --- js/tinyDOM.js | 74 +++++++++++++++++++++++---------------------------- test.html | 10 ++++--- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/js/tinyDOM.js b/js/tinyDOM.js index a87ff40..c0e64c8 100644 --- a/js/tinyDOM.js +++ b/js/tinyDOM.js @@ -1,19 +1,21 @@ //Prevent pollution of global namespace with closure -(function(){ - var tinyDOM = function(selector){ +(function() { + 'use strict'; + + var tinyDOM = function(selector) { return new tinyDOMFunction(selector); - } + }; var tinyDOMFunction = function(selector) { - if(selector === null || typeof(selector) === 'undefined'){ + if (selector === null || typeof (selector) === 'undefined') { this.length = 0; - } else if(typeof(selector) === 'string'){ + } else if(typeof(selector) === 'string') { var elements = document.querySelectorAll(selector); this.length = elements.length; - for(var i = 0; i < elements.length; i++){ + for(var i = 0; i < elements.length; i++) { var e = elements.item(i); - if(typeof(e.td_prop) === 'undefined'){ + if(typeof(e.td_prop) === 'undefined') { e.td_prop = { isHidden: false }; @@ -31,14 +33,14 @@ tinyDOM.fn = tinyDOMFunction.prototype = { each: function(fn) { var l = this.length; - while(l--){ + while(l--) { fn(l, this[l], this); } return this; }, hide: function(){ - this.each(function(i, e){ - if(!e.td_prop.isHidden){ + this.each(function(i, e) { + if(!e.td_prop.isHidden) { e.style.td_previousDisplay = e.style.display; e.style.display = 'none'; e.td_prop.isHidden = true; @@ -46,10 +48,10 @@ }); return this; }, - show: function(){ - this.each(function(i, e){ + show: function() { + this.each(function(i, e) { if(e.td_prop.isHidden === true){ - if(typeof(e.style.td_previousDisplay) !== 'undefined'){ + if(typeof(e.style.td_previousDisplay) !== 'undefined') { e.style.display = e.style.td_previousDisplay; } else { e.style.display = 'block'; @@ -59,22 +61,22 @@ }); return this; }, - on: function(ev, fn){ - this.each(function(i, e){ + on: function(ev, fn) { + this.each(function(i, e) { e.addEventListener(ev, fn); }); return this; }, - first: function(){ - if(typeof(this[0]) !== 'undefined'){ + first: function() { + if(typeof(this[0]) !== 'undefined') { return this[0]; } else { return null; } }, - data: function(key, value){ + data: function(key, value) { var e = this[0]; - if(typeof(value) !== 'undefined'){ + if(typeof(value) !== 'undefined') { e.setAttribute('data-'+key, value); return this; } else { @@ -83,16 +85,16 @@ } } - tinyDOM.exists = function(obj){ + tinyDOM.exists = function(obj) { return obj !== null && typeof(obj) !== 'undefined'; } - tinyDOM.merge = function(json1, json2){ - if(!this.exists(json1) || !this.exists(json2)){ + tinyDOM.merge = function(json1, json2) { + if(!this.exists(json1) || !this.exists(json2)) { return null; } else { for(var prop in json2) { - if(json2.hasOwnProperty(prop)){ + if(json2.hasOwnProperty(prop)) { json1[prop] = json2[prop]; } } @@ -100,7 +102,7 @@ } } - tinyDOM.ajax = function(options){ + tinyDOM.ajax = function(options) { var req = new XMLHttpRequest(); var _this = this; @@ -113,25 +115,17 @@ responseType: 'text', data: null, headers: [], - success: null, - error: null + callbacks: {} } this.merge(params, options); req.responseType = params.responseType; - req.onreadystatechange = function(){ - if(req.readyState > 1){ - if(req.status === 200){ - if (req.readyState === 4 && _this.exists(params.success)){ - params.success(req.response, req); - } - } else { - req.abort(); - if(_this.exists(params.error)){ - params.error({status: req.status, message: req.statusText}, req); - } + if(this.exists(params.callbacks)) { + for(var ev in params.callbacks) { + if(params.callbacks.hasOwnProperty(ev)) { + req.addEventListener(ev, params.callbacks[ev]); } } } @@ -144,7 +138,7 @@ params.password ); - for(var i = 0; i < params.headers.length; i++){ + for(var i = 0; i < params.headers.length; i++) { req.setRequestHeader(params.headers[i].header, params.headers[i].value); } @@ -152,10 +146,10 @@ return req; }; - if(!window.μ){ + if(!window.μ) { window.μ = tinyDOM; } - if(!window.mu){ + if(!window.mu) { window.mu = tinyDOM; } })(); diff --git a/test.html b/test.html index 4f9a54c..d46a068 100644 --- a/test.html +++ b/test.html @@ -13,7 +13,7 @@ so it would be <span class='important'>bad</span> if any of them dissapeared. </p> <p id='emptydiv'> - + </p> </div> @@ -63,9 +63,11 @@ console.log(μ(this).data('href')); μ.ajax({ url: url, - success: function(data){ - console.log(data); - μ('#emptydiv').first().innerHTML = data; + callbacks: { + load: function(data){ + console.log(data); + μ('#emptydiv').first().innerHTML = data; + } } }); }); -- GitLab