Compile your javascript into minispade modules using the asset pipeline
Ruby JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
vendor/assets/javascripts
.gitignore
Gemfile
LICENCE
README.md
Rakefile
minispade-rails.gemspec

README.md

minispade-rails Build Status Dependency Status

For large JavaScript applications, you need to include and parse alot of JavaScript just to start showing something on the page. minispade-rails allows you to defer the parsing of parts of your application until you need it.

minispade-rails allows you to compile your javascript files into strings that can be required at runtime using minispade.js

To understand the problem that minispade-rails solves, read AMD is Not the Answer by @tomdale and Lazy evaluation of CommonJS modules by Tobie Langel

Getting started

Add the gem to your application Gemfile:

gem "minispade-rails"

Then replace require_tree with require_spade and things should "just work", for example:

//= require_tree ./your_application
// Becomes...

//= require minispade
//= require_spade ./your_application

You need to call //= require minispade to include minispade.js into your application. If you get "minispade is undefined" type errors, you're probably missing that line.

What does require_spade do?

Usually, require_tree will recursively go through your folder structure, compile the asset, and drop it into the current file. So you get something that looks like this:

// path/to/file_1.js
(function() {
   alert('File #1');
}).call(this)
// path/to/file_2.js
(function() {
   alert('File #2');
}).call(this)
// path/to/file_3.js
(function() {
   alert('File #3');
}).call(this)

What require_spade will do, is it will compile the asset, and drop the contents into a minispade.register call, like so:

minispade.register("path/to/file_1.js", "alert('File #1')");
minispade.register("path/to/file_2.js", "alert('File #2')");
minispade.register("path/to/file_3.js", "alert('File #3')");

Then during your application code, as you need access to "file_1", you simply call minispade.require like so:

minispade.require("path/to/file_1.js")

And it will execute the file. If you've already called minispade.require before on the same file, it will do nothing. Note that calling minispade.require is a synchronous function call.

Configuration

The only configuration option that minispade-rails supports at the moment is the ability to switch on/off using a string to defer the parsing of the JavaScript.

Setting MinispadeRails::Config.deferred in an initializer to true will cause the following output:

minispade.register("path/to/file_1.js", "alert('File #1')");

Setting it to false will give you something like this:

minispade.register("path/to/file_1.js", function() { alert('File #1') });

Sometimes, evaling code is actually slower than just parsing it. If you have a very small app, but want to future-proof the code, then setting deferred to false early on, will give you faster loading times.

See http://blog.sproutcore.com/faster-loading-through-eval/ for more details.

Contributors / Credits

Thanks to the following:

Note on Patches/Pull Requests

  1. Fork the project.
  2. Make your feature addition or bug fix.
  3. Add tests for it. This is important so I don't break it in a future version unintentionally.
  4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  5. Send me a pull request. Bonus points for topic branches.