July 2012
Intermediate to advanced
254 pages
6h 16m
English
Lazy initialization is a design pattern that allows us to delay expensive
processes until the first instance they are needed. An example of this is
the .ready() function in jQuery that only executes a callback once the DOM is
ready.
$(document).ready(function(){// The ajax request won't attempt to execute until// the DOM is readyvarjqxhr=$.ajax({url:"http://domain.com/api/",data:"display=latest&order=ascending"}).done(function(data)){$(".status").html("content loaded");console.log("Data output:"+data);});});
jQuery.fn.ready() is powered by
jQuery.bindReady(), seen below:
bindReady:function(){if(readyList){return;}readyList=jQuery.Callbacks("once memory");// Catch cases where $(document).ready() is called after the// browser event has already occurred.if(document.readyState==="complete"){// Handle it asynchronously to allow scripts the opportunity to delay readyreturnsetTimeout(jQuery.ready,1);}// Mozilla, Opera and webkit support this eventif(document.addEventListener){// Use the handy event callbackdocument.addEventListener("DOMContentLoaded",DOMContentLoaded,false);// A fallback to window.onload, that will always workwindow.addEventListener("load",jQuery.ready,false);// If IE event model is used}elseif(document.attachEvent){// ensure firing before onload,// maybe late but safe also for iframesdocument.attachEvent("onreadystatechange",DOMContentLoaded);// A fallback to ...