Permalink
Browse files

Expanded LABjs usage docs

  • Loading branch information...
1 parent b10f736 commit 9d5bdd56effcf4fa4b6451d24cabf2a94a546561 @rondevera rondevera committed Apr 22, 2011
Showing with 105 additions and 56 deletions.
  1. +4 −1 lib/asset_hat/js/vendors.rb
  2. +15 −3 lib/asset_hat_helper.rb
  3. +86 −52 test/asset_hat_helper_test.rb
@@ -218,7 +218,10 @@ def self.loader_js(loader, opts)
is_last = i >= urls.length - 1
lines << " script('#{url}').wait()#{is_last ? ';' : '.'}"
# Use `wait()` for each script to load in parallel, but
- # preserve execution order by default.
+ # preserve execution order by default. Alternatively, call
+ # `$LAB.setOptions({AlwaysPreserveOrder:true})` at the start
+ # of the chain, but if the list of bundles to include is short,
+ # this may use even more bytes.
end
lines.join("\n")
end
View
@@ -251,8 +251,8 @@ def include_css(*args)
# include_js 'json2', 'application', :only_url => true
# => ['/javascripts/json2.js', '/javascripts/application.js']
#
- # Load JS files with LABjs (You must first download the latest LABjs
- # manually from <http://labjs.com/>):
+ # Load JS files with {LABjs}[http://labjs.com] (hosted either from cdnjs or
+ # your own web server, if found in <code>public/javascripts/</code>):
#
# # config/assets.yml:
# js:
@@ -262,13 +262,25 @@ def include_css(*args)
#
# # Usage:
# include_js :jquery, :bundle => 'application', :loader => :lab_js
- # => <script src="/javascripts/LAB-1.x.x.min.js" ...></script>
+ # => <script src="http://ajax.cdnjs.com/.../1.x.x/LAB.min.js" ...></script>
# <script type="text/javascript">
# window.$LABinst=$LAB.
# script('http://ajax.googleapis.com/.../jquery.min.js').wait().
# script('/javascripts/bundles/application.min.js').wait();
# </script>
#
+ # # For advanced fine-tuning, build the LABjs calls manually (based on
+ # # example from http://labjs.com/documentation.php ):
+ # <script>
+ # window.$LABinst = $LAB.
+ # script('<%= include_js 'framework', :only_url => true %>').wait().
+ # script('<%= include_js 'plugin.framework.js',
+ # :only_url => true %>').
+ # script('<%= include_js 'myplugin.framework.js',
+ # :only_url => true %>').wait().
+ # script('<%= include_js 'init.js', :only_url => true %>').wait();
+ # </script>
+ #
# # If you want to execute an inline <script> block that relies on any
# # of these dependencies, use the JS variable `window.$LABinst`.
# # Example (using jQuery to handle when DOM is ready):
@@ -888,59 +888,93 @@ class AssetHatHelperTest < ActionView::TestCase
end
teardown { ENV['RAILS_ASSET_ID'] = nil }
- should 'render with caching disabled' do
- loader_filename = "LAB-#{@lab_js_version}.min.js"
- vendor_filename = "jquery-#{@jquery_version}.min.js"
-
- flexmock(AssetHat).should_receive(:asset_exists?).
- with(loader_filename, :js).and_return(true)
- flexmock(AssetHat).should_receive(:asset_exists?).
- with(vendor_filename, :js).and_return(true)
- assert AssetHat.asset_exists?(vendor_filename, :js), 'Precondition'
-
- expected = %{<script src="/javascripts/#{loader_filename}" } +
- %{type="text/javascript"></script>\n}
- expected << %{<script type="text/javascript">\n}
- expected << "window.$LABinst=$LAB.\n"
- expected << " script('/javascripts/#{vendor_filename}').wait().\n"
- expected << " script('/javascripts/foo.js').wait().\n"
- expected << " script('/javascripts/js-file-1-1.js').wait().\n"
- expected << " script('/javascripts/js-file-1-2.js').wait().\n"
- expected << " script('/javascripts/js-file-1-3.js').wait().\n"
- expected << " script('/javascripts/js-file-2-1.js').wait().\n"
- expected << " script('/javascripts/js-file-2-2.js').wait().\n"
- expected << " script('/javascripts/js-file-2-3.js').wait();\n"
- expected << '</script>'
-
- assert_equal expected, include_js(:jquery, 'foo',
- :bundles => %w[js-bundle-1 js-bundle-2],
- :loader => :lab_js)
- end
+ context 'with local requests' do
+ should 'render with caching disabled' do
+ loader_filename = "LAB-#{@lab_js_version}.min.js"
+ vendor_filename = "jquery-#{@jquery_version}.min.js"
- should 'render with caching enabled and remote vendors' do
- flexmock(AssetHat, :consider_all_requests_local? => false)
- lab_js_url = 'http://ajax.cdnjs.com/ajax/libs/labjs/' +
- @lab_js_version + '/LAB.min.js'
- jquery_url = 'http://ajax.googleapis.com/ajax/libs/jquery/' +
- @jquery_version + '/jquery.min.js'
-
- expected = %{<script src="#{lab_js_url}" } +
- %{type="text/javascript"></script>\n}
- expected << %{<script type="text/javascript">\n}
- expected << "window.$LABinst=$LAB.\n"
- expected << " script('#{jquery_url}').wait().\n"
- expected << " script('/javascripts/foo.js').wait().\n"
- expected << " script('/javascripts/" +
- "bundles/js-bundle-1.min.js').wait().\n"
- expected << " script('/javascripts/" +
- "bundles/js-bundle-2.min.js').wait();\n"
- expected << '</script>'
-
- assert_equal expected, include_js(:jquery, 'foo',
- :bundles => %w[js-bundle-1 js-bundle-2],
- :loader => :lab_js,
- :cache => true)
- end
+ flexmock(AssetHat).should_receive(:asset_exists?).
+ with(loader_filename, :js).and_return(true)
+ flexmock(AssetHat).should_receive(:asset_exists?).
+ with(vendor_filename, :js).and_return(true)
+ assert AssetHat.asset_exists?(vendor_filename, :js),
+ 'Precondition'
+
+ expected = %{<script src="/javascripts/#{loader_filename}" } +
+ %{type="text/javascript"></script>\n}
+ expected << %{<script type="text/javascript">\n}
+ expected << "window.$LABinst=$LAB.\n"
+ expected << " script('/javascripts/#{vendor_filename}').wait().\n"
+ expected << " script('/javascripts/foo.js').wait().\n"
+ expected << " script('/javascripts/js-file-1-1.js').wait().\n"
+ expected << " script('/javascripts/js-file-1-2.js').wait().\n"
+ expected << " script('/javascripts/js-file-1-3.js').wait().\n"
+ expected << " script('/javascripts/js-file-2-1.js').wait().\n"
+ expected << " script('/javascripts/js-file-2-2.js').wait().\n"
+ expected << " script('/javascripts/js-file-2-3.js').wait();\n"
+ expected << '</script>'
+
+ assert_equal expected, include_js(:jquery, 'foo',
+ :bundles => %w[js-bundle-1 js-bundle-2],
+ :loader => :lab_js)
+ end
+
+ should 'render with caching disabled and remote vendor ' +
+ 'if local loader vendor is missing' do
+ loader_filename = "LAB-#{@lab_js_version}.min.js"
+ vendor_filename = "jquery-#{@jquery_version}.min.js"
+ loader_url = 'http://ajax.cdnjs.com/ajax/libs/labjs/' +
+ @lab_js_version + '/LAB.min.js'
+
+ flexmock(AssetHat).should_receive(:asset_exists?).
+ with(loader_filename, :js).and_return(false)
+ flexmock(AssetHat).should_receive(:asset_exists?).
+ with(vendor_filename, :js).and_return(true)
+ assert AssetHat.asset_exists?(vendor_filename, :js),
+ 'Precondition'
+
+ expected = %{<script src="#{loader_url}" } +
+ %{type="text/javascript"></script>\n}
+ expected << %{<script type="text/javascript">\n}
+ expected << "window.$LABinst=$LAB.\n"
+ expected << " script('/javascripts/#{vendor_filename}').wait().\n"
+ expected << " script('/javascripts/foo.js').wait();\n"
+ expected << '</script>'
+
+ assert_equal expected,
+ include_js(:jquery, 'foo', :loader => :lab_js)
+ end
+ end # context 'with local requests'
+
+ context 'with remote requests' do
+ setup do
+ flexmock(AssetHat, :consider_all_requests_local? => false)
+ end
+
+ should 'render with caching enabled and remote vendors' do
+ lab_js_url = 'http://ajax.cdnjs.com/ajax/libs/labjs/' +
+ @lab_js_version + '/LAB.min.js'
+ jquery_url = 'http://ajax.googleapis.com/ajax/libs/jquery/' +
+ @jquery_version + '/jquery.min.js'
+
+ expected = %{<script src="#{lab_js_url}" } +
+ %{type="text/javascript"></script>\n}
+ expected << %{<script type="text/javascript">\n}
+ expected << "window.$LABinst=$LAB.\n"
+ expected << " script('#{jquery_url}').wait().\n"
+ expected << " script('/javascripts/foo.js').wait().\n"
+ expected << " script('/javascripts/" +
+ "bundles/js-bundle-1.min.js').wait().\n"
+ expected << " script('/javascripts/" +
+ "bundles/js-bundle-2.min.js').wait();\n"
+ expected << '</script>'
+
+ assert_equal expected, include_js(:jquery, 'foo',
+ :bundles => %w[js-bundle-1 js-bundle-2],
+ :loader => :lab_js,
+ :cache => true)
+ end
+ end # context 'with remote requests'
end # context 'with LABjs version config, vendor, and multiple bundles'
end # context 'with LABjs'

0 comments on commit 9d5bdd5

Please sign in to comment.