Wolox

The Wolox engineering blog.

Introducing Loadjs

Today we have released a new version of our Loadjs gem. This gem allows users to include page specific Javascript code in their Rails application without losing all the magic provided by sprockets.

So after installing the gem you can include page specific Javascript right away. For example lets say that you want to execute a piece of code only for the ‘foo’ controller and ‘bar’ action. You can do it like this:

1
2
3
load("foo#bar", function(controller, action) {
  alert("Hello World!");
});

You can also use a hash option instead of the string to set the controller and the action.

1
2
3
load({controller: "foo", action: "bar"}, function(controller, action) {
  alert("Hello World!");
});

This allows users to omit the action option which will execute the function for all actions of the given controller. For example if you want to execute a piece of code for all the actions of the ‘foo’ controller you can do it like this:

1
2
3
load({controller: "foo"}, function(controller) {
  alert("Hello World!");
});

Finally you can also specify several controllers and actions. For example:

1
2
3
4
5
6
7
8
9
load({
  controllers: {
    controller_1: ['action_1', 'action_2', 'action_3'],
    controller_2: ['action_1'],
    controller_3: []
  }
}, function(controller) {
  alert("Hello World!");
});

This will show an alert for the actions ‘action_1’, ‘action_2’ and ‘action_3’ of the controller ‘controller_1’, the action ‘action_1’ of the controller ‘controller_2’ and all the action of the controller ‘controller_3’.

Hope you find this useful. We would like to hear your feedback and pull requests are welcomed.

Comments