Prepend to javascript_include_tag :defaults

JavaScript, Ruby on Rails No Comments »

Yes, there is register_javascript_include_default but it will only append to your sources.

To prepend (I’ll use this to have the excellent JS.Class libraries loaded before anything else), proceed like so:

While in Rails 3, you can set

config.action_view.javascript_expansions[:defaults].unshift(’my_prepend_1′, my_prepend_2′)

in your application.rb file,

in Rails 2.3.x you create an initializer (e.g. config/initializers/my_js_defaults.rb) and reset the JAVASCRIPT_DEFAULT_SOURCES constant as so:

module ActionView::Helpers::AssetTagHelper
  JAVASCRIPT_DEFAULT_SOURCES = (remove_const :JAVASCRIPT_DEFAULT_SOURCES).unshift(’my_prepend_1′, ‘my_prepend_2′)
   reset_javascript_include_default
end

This post gave me the idea. It concerns Rails 3 (before the above config.action_view.javascript_expansions which was introduced in Rails 3 RC)  but the code also works with Rails 2.3.x.

 

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Learning Javascript with Crockford and Spidermonkey

JavaScript No Comments »

As JavaScript is the language I’m planning to learn more in-depth this year, primarily to approach the latest map-reduce API’s of the latests NoSQL databases (CouchDB, riak etc.), I’ve recently made good progress thanks to two invaluable resources:

Crockford Videos

First, Douglas Crockford’s lecture videos at the YUI theater. He has created three separate series, The JavaScript Programming language, Advanced JavaScript. and more recently, Crockford on JavaScript, all available on the same page in the YUI theater. The third episode (Act III) of Crockford on Javascript gives you a tour de force of functional programming and is probably the most valuable of all the videos for advanced programmers.

He also states in his first lecture that there are no good books on JavaScript, except perhaps a single one, JavaScript: The Definitive Guide By David Flannagan. You may also want to check out Crockford’s own books which I haven’t had the chance to read yet.

Finally, always use Crockford’s jslint to check your javascript code quality. It’s a great teaching tool in and of itself and will help you find all those missing semi-colons and curly braces.

Spidermonkey Command Line

Interactively checking code on the command line is standard practice in the ruby and python universes. Now you can easily have the same code checker at your fingertips with JavaScript. In Ubuntu (8.04 and higher) simply

sudo apt-get install spidermonkey-bin

, and then type

> js

to get the beautiful prompt:

js>

Here are some tips for working with the spidermonkey command line. And, to use jslint from the command line, check out this post.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in