<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Exploring the land of code.</description><title>&lt; Pixel Perfect /&gt;</title><generator>Tumblr (3.0; @mickeyelliott)</generator><link>http://mickeyelliott.com/</link><item><title>Modernizr &amp; Plugin Detection</title><description>&lt;p&gt;&lt;a href="http://www.modernizr.com/" title="Modernizr" target="_blank"&gt;Modernizr&lt;/a&gt; is a popular javascript library that helps you build the next generation of html5 and css3-powered websites. If your not familiar with it and your a developer, I recommend you visit there website.&lt;/p&gt;
&lt;p&gt;Apart from its amazing feature detection abilities I&amp;#8217;ve decided to play with its api a little. In a recent project of mine I wanted to seporate my javascript logic into 2 main categories to help keep clean seporation in my MVC app. My application varies in script(plugin) loading on each page. So I decided to use Modernizr&amp;#8217;s feature detection abilities to load jquery plugins to decrease the overhead loading time on my pages.&lt;/p&gt;
&lt;p&gt;Modernizrs api contains an method called addTest().&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
    Modernizr.addTest(str, fn)
    Modernizr.addTest(str, bool)
    Modernizr.addTest({str: fn, str2: fn2})
    Modernizr.addTest({str: bool, str2: fn})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#8217;ll add a class name to any element on the page, I&amp;#8217;ll run a test. If Modernizr finds that specific class name, it means that that feature(plugin) exsists and needs to load its dependencies.&lt;/p&gt;
&lt;p&gt;First I&amp;#8217;ll add the new features to Modernizr:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
/* Adding any of the class names to any element on the page 
   will load the dependencies for that plugin. */

Modernizr.addTest('pluginSlider', $(".pluginSlider").length == 1);
Modernizr.addTest('pluginTipped', $(".pluginTipped").length == 1);
Modernizr.addTest('pluginFancybox', $(".pluginFancybox").length == 1);
Modernizr.addTest('pluginSuperfish', $(".pluginSuperfish").length == 1);
Modernizr.addTest('pluginIsotope', $(".pluginIsotope").length == 1);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next run the tests that determine what plugins are on the page.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Modernizr.load([
    {
        test: Modernizr.pluginSlider, // This will be truthy or falsy
        nope: ['/js/libs/slides.min.jquery.js']
    },
    {
        test: Modernizr.pluginTipped,
        nope: ['/js/libs/spinners.js', '/js/libs/tipped.js', '/css/tipped.css'],
        callback: function (url, result, key) { 
                      Modernizr.load([{ 
                          test: Modernizr.canvas, nope: ['/js/libs/excanvas.js']
                      }]); 
                  }
    },
    {
        test: Modernizr.pluginIsotope,
        nope: ['/js/libs/jquery.isotope.min.js']
    }
]);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great! Scripts will now load if a class name such as &amp;#8220;.pluginSlider&amp;#8221; is on some element. Except one more thing that needs to be addressed. I want to separate the page specific logic. I like to develop using closures so what I can do is initiate whatever my page js object is. By adding the complete() method to the end of my tests, I can initiate my page object which has all my page specific logic in it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Modernizr.load([
    {
        test: Modernizr.pluginSlider, // This will be truthy or falsy
        nope: ['/js/libs/slides.min.jquery.js']
    },
    {
        test: Modernizr.pluginTipped,
        nope: ['/js/libs/spinners.js', '/js/libs/tipped.js', '/css/tipped.css'],
        callback: function (url, result, key) { 
                      Modernizr.load([{ 
                          test: Modernizr.canvas, nope: ['/js/libs/excanvas.js']
                      }]); 
                  }
    },
    {
        test: Modernizr.pluginIsotope,
        nope: ['/js/libs/jquery.isotope.min.js'],

        /* HERE IS THE INIT OBJECT */
        complete: function () {
            /* Run this after everything in this group has downloaded
            and executed, as well everything in all previous groups */

            /* Each page should contain the object myapp else we just 
               fall back to an empty object */

            if (typeof myapp == 'function') {
                var app = new myapp();
                app.init();
            }


        }
    }
]);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now on my page all I need to have is the closure model that initiates. To understand javascript closures view my other &lt;a href="http://mickeyelliott.com/post/15714241032/javascript-closures" target="_blank"&gt;post here&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
var myapp = (function () {

    var root = this;
    
    root.init = function(){
        /* CALL YOUR PLUGINS HERE */
        $("pluginSlider").slider();
    };

});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Summary: Clean separation of global dependencies and page specific logic. Improved performance by loading only scripts that are actually needed.&lt;/p&gt;</description><link>http://mickeyelliott.com/post/15729174854</link><guid>http://mickeyelliott.com/post/15729174854</guid><pubDate>Thu, 12 Jan 2012 10:08:00 -0800</pubDate><category>Script Loading</category><category>Modernizr</category><category>Jquery</category><category>Plugins</category><category>Javascript</category><category>OOP</category><category>Detection</category></item><item><title>JavaScript Closures</title><description>&lt;div&gt;
&lt;p&gt;JavaScript objects are at the heart of every application. Unfortunately theres alot of misuse of objects. Before diving into this brief explination I want to start by saying &amp;#8220;If you understand the framework, the rest is just syntax&amp;#8221;. Dont get caught up on trying to understand all of the syntax.&lt;/p&gt;
&lt;p&gt;Think of an object as a container. That containers content should be relevant to its purpose. &lt;span&gt;A container with a collection of properties and methods.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Lets use the &lt;strong&gt;car&lt;/strong&gt; example. To begin were going to create a &lt;strong&gt;model&lt;/strong&gt;&lt;span&gt; that will represent what a car is. Then we will actually create a car when finished.&lt;/span&gt; The car being the JavaScript object:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
var car= (function(){    /* I'm inside of the car. */    });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A car has several properties like fuel level, tire pressure. It may even have safety features like traction control or airbags.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
var car = (function(){

    var root= this;

    root.properties = {
        fuelLevel: "100%",
        tirePressure: "high",
        headLights: 'on',
        engine:"off"
    };

});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The car now contains the properties we can later use. Cars aren&amp;#8217;t just for show though. They move, accelerate, brake. Maybe even have a few other features like heat or sunroof.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
var car = (function(){

    var root= this;

    root.properties = {
        fuelLevel: "100%",
        tirePressure: "high",
        headLights: 'on',
        engine:"off"
    };

    root.actions = {
        start:function () { root.properties.engine = "on"; alert("car started"); },
        drive:function () { alert("driving away"); },
        turn:function () { },
        signal:function () { }
    };

});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have finished our closure, lets use the car!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
var myCar= new car();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My myCar object is now using the car model we designed. Let&amp;#8217;s start it up.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
if(  myCar.properties.fuelLevel &amp;gt; 0 ) {
    
    myCar.actions.Start();

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now lets drive our car. But wait. You can&amp;#8217;t drive a car that&amp;#8217;s not started so let&amp;#8217;s check to see if it&amp;#8217;s started first.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
if( myCar.properties.engine == "on" ) {
    
    myCar.actions.drive();

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Summary: &lt;br/&gt;By enclosing related javascript properties and methods, your enabling better reuse of your code. This will in the end improve performance.&lt;/p&gt;
&lt;/div&gt;</description><link>http://mickeyelliott.com/post/15714241032</link><guid>http://mickeyelliott.com/post/15714241032</guid><pubDate>Wed, 11 Jan 2012 22:45:00 -0800</pubDate><category>javascript</category><category>closures</category><category>objects</category><category>reuse</category></item><item><title>"Have a little success, and theres always a fool to tell you how talented you are."</title><description>“Have a little success, and theres always a fool to tell you how talented you are.”</description><link>http://mickeyelliott.com/post/1541405379</link><guid>http://mickeyelliott.com/post/1541405379</guid><pubDate>Thu, 11 Nov 2010 00:23:47 -0800</pubDate></item></channel></rss>

