Skip to content

Instantly share code, notes, and snippets.

@diogofriggo
Created January 17, 2013 03:25
Show Gist options
  • Save diogofriggo/4553315 to your computer and use it in GitHub Desktop.
Save diogofriggo/4553315 to your computer and use it in GitHub Desktop.
Registering ExtJS controllers automatically with Embedded Ruby
//Whenever I start a new story there comes the excessive boilerplating that tags along, a series of mini steps
//to get you going, one such step is registering your ExtJs controller on App.js, it looks like this:
Ext.application
name: 'Fidget'
autoCreateViewport: true
controllers: [
'YourController',
'HundredsOfOtherControllers'
]
//now given my lazyness has caught up with me today, I decided it was time to automate that step
//using Embedded Ruby (erb)
//File: App.js.coffee.erb
Ext.application
name: 'Fidget'
autoCreateViewport: true
controllers: [
<%
path = 'app/assets/javascripts/controller'
root = "Fidget.controller."
controllers = Dir.glob("#{path}/**/*")
.select { |file| File.file? file and file.end_with? '.js' }
.map { |file| File.read(file) }
.map { |file| file.match(/#{root}(?<name>[\w\.]+)/)[:name] }
.map { |file| "'#{file}'" }
.join(",")
%>
<%= controllers %>
]
//Now, that controllers array will get filled with all the 129 controllers the app has as of today
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment