Last active
January 8, 2016 19:13
-
-
Save coryodaniel/5db8b567db8c8b3fdd63 to your computer and use it in GitHub Desktop.
Queue up functions to run against a specific controller/action pair
This file contains hidden or 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
| var Page = Page || {}; | |
| (function(Page) { | |
| 'use strict'; | |
| var routeQueue = []; | |
| Page.runRouteQueue = function(){ | |
| routeQueue.forEach(function(cb, index, array) { | |
| cb(); | |
| }); | |
| }; | |
| Page.onRoute = function(){ | |
| var controller = arguments[0]; | |
| var action = arguments.length == 3 ? arguments[1] : null; | |
| var cb = arguments.length == 3 ? arguments[2] : arguments[1]; | |
| var data_controller = document.body.getAttribute('data-controller'); | |
| var data_action = document.body.getAttribute('data-action'); | |
| if( data_controller == controller && ( !action || data_action == action ) ){ | |
| if(cb){ | |
| routeQueue.push(cb); | |
| } else { | |
| routeQueue.push(function(){ | |
| console.log(arguments); | |
| }); | |
| } | |
| } | |
| }; | |
| })(Page); | |
| // testin' | |
| document.body.setAttribute('data-controller','orders'); | |
| document.body.setAttribute('data-action','show'); | |
| Page.onRoute('orders', function(){ | |
| console.log("Orders") | |
| }); | |
| Page.onRoute('orders','show', function(){ | |
| console.log("Orders#show") | |
| }); | |
| Page.onRoute('nope','show', function(){ | |
| console.log("I shouldn't print") | |
| }); | |
| Page.runRouteQueue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment