Skip to content

Instantly share code, notes, and snippets.

@smarquez1
Created October 4, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarquez1/f76756fd9d8e9f392f5635087bc678ab to your computer and use it in GitHub Desktop.
Save smarquez1/f76756fd9d8e9f392f5635087bc678ab to your computer and use it in GitHub Desktop.
Page specific javascript on Rails
//Based on https://www.driftingruby.com/episodes/page-specific-javascript-in-ruby-on-rails#comments
// Usage:
// if(!page.isControllerAndAction('users', 'index')) return;
var Page, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Page = (function() {
function Page() {
this.action = bind(this.action, this);
this.controller = bind(this.controller, this);
}
Page.prototype.controller = function() {
return $('meta[name=psj]').attr('controller');
};
Page.prototype.action = function() {
return $('meta[name=psj]').attr('action');
};
Page.prototype.isController = function(controller) {
return controller === this.controller()
};
Page.prototype.isAction = function(action) {
return action === this.action()
};
Page.prototype.isControllerAndAction = function(controller, action) {
return this.isController(controller) && this.isAction(action)
};
return Page;
})();
this.page = new Page;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment