<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: LABjs: how to deal with inline code</title>
	<atom:link href="http://blog.getify.com/labjs-how-to-deal-with-inline-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/</link>
	<description>javascript, performance, and ui musings</description>
	<lastBuildDate>Mon, 07 May 2012 16:42:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: getify</title>
		<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/comment-page-1/#comment-1539</link>
		<dc:creator>getify</dc:creator>
		<pubDate>Mon, 01 Nov 2010 14:57:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.getify.com/?p=122#comment-1539</guid>
		<description>@Tom-
If I understand you correctly, I think this blog post helps explain how you might accomplish this:

http://blog.getify.com/2010/02/simulated-chaining-in-javascript/

Also, note that $LAB.block() is never a correct pattern, as &quot;block&quot; should never be the first function call in a $LAB chain. I imagine you&#039;re probably doing that because you&#039;re trying to &quot;continue&quot; the previous chain, but unfortunately you&#039;re not doing that, you&#039;re creating a new chain. The above link shows you how you can programatically build up a chain with whatever conditional logic you want, and then simulate the chain with a for-loop, which I imagine will accomplish the goal you have of extending a current chain based on conditional logic. Have a look and lemme know if that helps.

Lastly, note that &quot;block&quot; is now a deprecated API call, replaced with &quot;wait&quot;. The next version of LABjs will not have &quot;block&quot; anymore.</description>
		<content:encoded><![CDATA[<p>@Tom-<br />
If I understand you correctly, I think this blog post helps explain how you might accomplish this:</p>
<p><a href="http://blog.getify.com/2010/02/simulated-chaining-in-javascript/" rel="nofollow">http://blog.getify.com/2010/02/simulated-chaining-in-javascript/</a></p>
<p>Also, note that $LAB.block() is never a correct pattern, as &#8220;block&#8221; should never be the first function call in a $LAB chain. I imagine you&#8217;re probably doing that because you&#8217;re trying to &#8220;continue&#8221; the previous chain, but unfortunately you&#8217;re not doing that, you&#8217;re creating a new chain. The above link shows you how you can programatically build up a chain with whatever conditional logic you want, and then simulate the chain with a for-loop, which I imagine will accomplish the goal you have of extending a current chain based on conditional logic. Have a look and lemme know if that helps.</p>
<p>Lastly, note that &#8220;block&#8221; is now a deprecated API call, replaced with &#8220;wait&#8221;. The next version of LABjs will not have &#8220;block&#8221; anymore.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/comment-page-1/#comment-1537</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Mon, 01 Nov 2010 14:48:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.getify.com/?p=122#comment-1537</guid>
		<description>Hi

We have the following scenario on our site where we want to combine an &quot;if&quot; statement (in the example below, we&#039;re checking the variable &quot;allowExternalScripts&quot;) with loading in script files. I&#039;ve read your comments on trying to only have one occurence of $LAB in the code but haven&#039;t been able to work out a way to include further script files once an operator has executed as part of the $LAB flow. Do you have any suggestions how this could be refined (ideally 4 &amp; 5 would be combined as I expect initPage could be called before 4 has finished)?

&lt;pre class=&quot;code&quot;&gt;
// 1
var postInitPage = function() {

    if (allowExternalScripts) {
        $LAB
        .block()
            .script(document.location.protocol + &#039;//external-file-c.js&#039;)
            .script(document.location.protocol + &#039;//external-file-d.js&#039;);
    }
        
}

// 2
function initDom() {

    // initialise DOM elements here

}

// 3
function initPage() {

    $(document).ready(function() {
        initDom();
        postInitPage();
    });

}

// 4
if (allowExternalScripts) {
    $LAB
    .block()
        .script(document.location.protocol + &#039;//external-file-a.js&#039;)
        .script(document.location.protocol + &#039;//external-file-b.js&#039;);
}

// 5
$LAB
.script(document.location.protocol + &#039;//internal-file-a.js&#039;)
    .block()
    .script(document.location.protocol + &#039;//internal-file-b.js&#039;)
    .script(document.location.protocol + &#039;//internal-file-c.js&#039;)
    .script(document.location.protocol + &#039;//internal-file-d.js&#039;)
    .script(document.location.protocol + &#039;//internal-file-e.js&#039;)
        .block(function() {
            initPage();
        });
&lt;/pre&gt;
	
Many thanks
Tom</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>We have the following scenario on our site where we want to combine an &#8220;if&#8221; statement (in the example below, we&#8217;re checking the variable &#8220;allowExternalScripts&#8221;) with loading in script files. I&#8217;ve read your comments on trying to only have one occurence of $LAB in the code but haven&#8217;t been able to work out a way to include further script files once an operator has executed as part of the $LAB flow. Do you have any suggestions how this could be refined (ideally 4 &amp; 5 would be combined as I expect initPage could be called before 4 has finished)?</p>
<pre class="code">
// 1
var postInitPage = function() {

    if (allowExternalScripts) {
        $LAB
        .block()
            .script(document.location.protocol + '//external-file-c.js')
            .script(document.location.protocol + '//external-file-d.js');
    }

}

// 2
function initDom() {

    // initialise DOM elements here

}

// 3
function initPage() {

    $(document).ready(function() {
        initDom();
        postInitPage();
    });

}

// 4
if (allowExternalScripts) {
    $LAB
    .block()
        .script(document.location.protocol + '//external-file-a.js')
        .script(document.location.protocol + '//external-file-b.js');
}

// 5
$LAB
.script(document.location.protocol + '//internal-file-a.js')
    .block()
    .script(document.location.protocol + '//internal-file-b.js')
    .script(document.location.protocol + '//internal-file-c.js')
    .script(document.location.protocol + '//internal-file-d.js')
    .script(document.location.protocol + '//internal-file-e.js')
        .block(function() {
            initPage();
        });
</pre>
<p>Many thanks<br />
Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Hakanson</title>
		<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/comment-page-1/#comment-535</link>
		<dc:creator>Kevin Hakanson</dc:creator>
		<pubDate>Wed, 18 Aug 2010 19:36:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.getify.com/?p=122#comment-535</guid>
		<description>I had a similar problem with multiple inline script blocks, but I was unable to combine them into one because they came from different components.  After I realized the chain was instance based, I &quot;continued&quot; the chain by keeping the reference in a global variable, and using it in my future inline script block.

Here&#039;s this posts sample code, rewritten with my technique:

&lt;pre class=&quot;code&quot;&gt;
  var instance = $LAB
  .script(&quot;framework.js&quot;)
  .script(&quot;myscript.js&quot;)
  .wait(function(){ myscript.init(); });


  instance.wait(function(){ 
    framework.init();
    framework.doSomething();
  });
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I had a similar problem with multiple inline script blocks, but I was unable to combine them into one because they came from different components.  After I realized the chain was instance based, I &#8220;continued&#8221; the chain by keeping the reference in a global variable, and using it in my future inline script block.</p>
<p>Here&#8217;s this posts sample code, rewritten with my technique:</p>
<pre class="code">
  var instance = $LAB
  .script("framework.js")
  .script("myscript.js")
  .wait(function(){ myscript.init(); });

  instance.wait(function(){
    framework.init();
    framework.doSomething();
  });
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arun Basnet</title>
		<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/comment-page-1/#comment-520</link>
		<dc:creator>Arun Basnet</dc:creator>
		<pubDate>Fri, 06 Aug 2010 23:15:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.getify.com/?p=122#comment-520</guid>
		<description>Great! 
Thank you so much! I guess I referred to quite old posts to have used .block. :D
The one I&#039;d referred to here :
http://ajaxian.com/archives/labjs-simple-abstraction-for-loading-dependencies-correctly
-- 
said quite clearly in the example:
old=&gt;





new=&gt;
&lt;pre class=&quot;code&quot;&gt;
$LAB
.script(&quot;jquery.js&quot;)
.block(function(){
      $LAB
      .script(&quot;jquery.ui.js&quot;)
      .script(&quot;myplugin.jquery.js&quot;)
      .block(function(){
            $LAB.script(&quot;initpage.js&quot;);
      });
});
&lt;/pre&gt;

I know its wrong now.
Thank you again

I think I&#039;m clear about it now. 

Thanks a lot again!</description>
		<content:encoded><![CDATA[<p>Great!<br />
Thank you so much! I guess I referred to quite old posts to have used .block. <img src='http://getiblog.2static.it/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
The one I&#8217;d referred to here :<br />
<a href="http://ajaxian.com/archives/labjs-simple-abstraction-for-loading-dependencies-correctly" rel="nofollow">http://ajaxian.com/archives/labjs-simple-abstraction-for-loading-dependencies-correctly</a><br />
&#8211;<br />
said quite clearly in the example:<br />
old=&gt;</p>
<p>new=&gt;</p>
<pre class="code">
$LAB
.script("jquery.js")
.block(function(){
      $LAB
      .script("jquery.ui.js")
      .script("myplugin.jquery.js")
      .block(function(){
            $LAB.script("initpage.js");
      });
});
</pre>
<p>I know its wrong now.<br />
Thank you again</p>
<p>I think I&#8217;m clear about it now. </p>
<p>Thanks a lot again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: getify</title>
		<link>http://blog.getify.com/labjs-how-to-deal-with-inline-code/comment-page-1/#comment-519</link>
		<dc:creator>getify</dc:creator>
		<pubDate>Fri, 06 Aug 2010 22:17:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.getify.com/?p=122#comment-519</guid>
		<description>@Arun- unfortunately, I don&#039;t know of any great examples out there other than this blog post and perhaps a few articles strewn about. I&#039;m still the main person promoting how LABjs actually works. Hope eventually more others will pick up the mantle.

To try and clarify for you: A single $LAB chain consists of a starting $LAB followed by a chain of one or more .script() calls and .wait() calls, interspersed as you need them to be. You should always try to avoid *multiple* $LAB calls, as they operate completely independently. Also, by nesting a $LAB inside a .wait(), you prevent that chain from even *loading* until the .wait() has executed. By doing a single chain with all your scripts, LABjs can load them *all* in parallel, but control their execution order based on where you have .wait() calls. That&#039;s what .wait() is for, to ensure *execution* order.

As for your $document.ready()... the use of $document.ready() is entirely separate from and orthagonal to the order of your scripts loading. It is only for making sure some logic doesn&#039;t run until the dom is ready. LAB script loads *do not* affect dom readiness at all (they don&#039;t hold it up) so there&#039;s no connection that you should make between sequencing $LAB with document.ready.

Instead, you should load all your scripts with LABjs and use .wait() where it&#039;s most appropriate to run code. If that code happens to be dependent on the dom-readiness, wrap the code (inside your .wait()) with a document.ready. In some cases, your scripts may load and execute before dom-ready, and thus your logic will protected to run later when the dom is ready. In other cases, dom-ready will pass before your scripts load and execute, and using document.ready() will essentially pass through and execute immediately.

If the code you need to wrap in $(document).ready() only cares about jquery, and not about the plugins, then do this:

&lt;pre class=&quot;code&quot;&gt;
$LAB
.script(&quot;jquery.js&quot;)
.wait(function(){
   $(document).ready(function() {  /* do something */ });
})
.script(&quot;plugin1.js&quot;)
.....
&lt;/pre&gt;

If the code you have to run needs the plugin to be there, then do it this way:
&lt;pre class=&quot;code&quot;&gt;
$LAB
.script(&quot;jquery.js&quot;)
.wait()
.script(&quot;plugin1.js&quot;)
.wait(function(){
   $(document).ready(function() {  /* do something */ });
});
&lt;/pre&gt;

Does that make sense more now?</description>
		<content:encoded><![CDATA[<p>@Arun- unfortunately, I don&#8217;t know of any great examples out there other than this blog post and perhaps a few articles strewn about. I&#8217;m still the main person promoting how LABjs actually works. Hope eventually more others will pick up the mantle.</p>
<p>To try and clarify for you: A single $LAB chain consists of a starting $LAB followed by a chain of one or more .script() calls and .wait() calls, interspersed as you need them to be. You should always try to avoid *multiple* $LAB calls, as they operate completely independently. Also, by nesting a $LAB inside a .wait(), you prevent that chain from even *loading* until the .wait() has executed. By doing a single chain with all your scripts, LABjs can load them *all* in parallel, but control their execution order based on where you have .wait() calls. That&#8217;s what .wait() is for, to ensure *execution* order.</p>
<p>As for your $document.ready()&#8230; the use of $document.ready() is entirely separate from and orthagonal to the order of your scripts loading. It is only for making sure some logic doesn&#8217;t run until the dom is ready. LAB script loads *do not* affect dom readiness at all (they don&#8217;t hold it up) so there&#8217;s no connection that you should make between sequencing $LAB with document.ready.</p>
<p>Instead, you should load all your scripts with LABjs and use .wait() where it&#8217;s most appropriate to run code. If that code happens to be dependent on the dom-readiness, wrap the code (inside your .wait()) with a document.ready. In some cases, your scripts may load and execute before dom-ready, and thus your logic will protected to run later when the dom is ready. In other cases, dom-ready will pass before your scripts load and execute, and using document.ready() will essentially pass through and execute immediately.</p>
<p>If the code you need to wrap in $(document).ready() only cares about jquery, and not about the plugins, then do this:</p>
<pre class="code">
$LAB
.script("jquery.js")
.wait(function(){
   $(document).ready(function() {  /* do something */ });
})
.script("plugin1.js")
.....
</pre>
<p>If the code you have to run needs the plugin to be there, then do it this way:</p>
<pre class="code">
$LAB
.script("jquery.js")
.wait()
.script("plugin1.js")
.wait(function(){
   $(document).ready(function() {  /* do something */ });
});
</pre>
<p>Does that make sense more now?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Content Delivery Network via getiblog.2static.it

Served from: blog.getify.com @ 2012-05-18 07:27:34 -->
