Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Last active January 8, 2016 19:13
Show Gist options
  • Select an option

  • Save coryodaniel/5db8b567db8c8b3fdd63 to your computer and use it in GitHub Desktop.

Select an option

Save coryodaniel/5db8b567db8c8b3fdd63 to your computer and use it in GitHub Desktop.
Queue up functions to run against a specific controller/action pair
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