Created
October 4, 2018 15:15
-
-
Save smarquez1/f76756fd9d8e9f392f5635087bc678ab to your computer and use it in GitHub Desktop.
Page specific javascript on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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