
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
NOTICE: We decided to rename this gem from rails-js
to rails-js-routes
as it was nearly impossible to find.
I worked on several Rails projects and there were many cases when it felt overkill to use a framework like Angular
or Ember
, because javascript was only needed in some parts of the application.
But by not using these frameworks I had to think of a way to structure the code by myself.
I came up with the conclusion to organize the javascript code like the ruby code is organized, that means: Split up in controllers, which are split up in actions.
rails-js-routes
we are able to define controllers
, as well as helpers
.actions
which are executed when the matching rails action is executed.view helper
to use in your layouts, which passes the routing information to rails-js-routes
.//= require routes
/*
* app/assets/javascripts/application.js
*/
Rails.app({
// Define global helpers here
helper: function() {
}
},
// Put in initial data
{
foo: 'bar'
},
// A function that is executed before every action
function() {
console.log('beforeAll() executed');
});
/*
* app/assets/javascripts/controllers/users.js
*/
Rails.controller('users', {
// actions here
new: function(h) {
// execute helper
h.awesomeHelper();
},
index: function(h) {
// Do the same as in new()
this.index(h);
}
},
{
// helpers here
awesomeHelper: function() {
}
});
/*
* app/assets/javascripts/controllers/admin/users.js
*/
Rails.controller('admin/users', {
new: function(h) {
// Execute action of foreign controller
R.action('users#new');
},
index: function(h) {
// Execute foreign helper with arguments
R.helper('users', 'awesomeHelper', 1, 2, 3);
// Execute global helper with arguments
R.global('helper', 1, 2, 3);
}
},{});
/*
* Use R singleton everywhere
*/
// Setting
R('answer', 42);
// Multiple levels
R('a.global.variable', 'foo');
// Getting
console.log(R('a.global.variable'));
/*
* app/assets/javascripts/namespaces/admin.js
*/
Rails.namespace('admin', function() {
// Will be executed on all admin routes (e.g. admin/users#new)
console.log('Hello Mr. Admin');
});
...just read in app/assets/javascripts/routes.js
. I tried my best and documented nearly every line of code :)
Install it as any other gem by putting this to your Gemfile
:
gem 'rails-js-routes'
In your layouts, put this line into the head
:
<%= js_routes %>
Also make sure to include routes
in your application.js
. If you are using Turbolinks, make sure to include routes
after turbolinks
.
Javascript tests are in test/dummy/spec/javascripts/rails-js-routes_spec.js
.
They are executed using rake teaspoon
(by using the teaspoon runner).
I'm still trying to get travis to execute the teaspoon tests properly...
Fork -> Commit -> Pull Request
This project uses the Airbnb Styleguide as our coding guideline. We think this is a good piece of work and worth spreading. In case of any doubts refer to the style used around.
FAQs
Unknown package
We found that rails-js-routes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.