Skip to content

Instantly share code, notes, and snippets.

@andrewmp1
Forked from simeonwillbanks/app.js.coffee
Created July 11, 2013 22:37
Show Gist options
  • Save andrewmp1/5979942 to your computer and use it in GitHub Desktop.
Save andrewmp1/5979942 to your computer and use it in GitHub Desktop.
# /code/railsapp/app/assets/javascripts/thing/app.js.coffee
#= require angular/templates
angular.module("thing", ["app.templates"]).value("appName", "thing")

AngularJS app JavaScript keyed by app name thing

/code/railsapp/app/assets/javascripts/thing/

AngularJS app templates keyed by app name thing

/code/railsapp/app/assets/templates/thing/

Shared AngularJS modules

/code/railsapp/lib/assets/javascripts/angular/

<%
# /code/railsapp/lib/assets/javascripts/angular/templates.js.coffee.erb
template_cache = []
# Prefill templateCache with latest templates
if Rails.env.production? || Rails.env.staging?
Dir.glob(Rails.root.join("app", "assets", "templates", "**", "*.haml")).each do |f|
pathname = Pathname(f)
template = {}
# /code/railsapp/app/assets/templates/thing -> assets/thing
template[:dirname] = pathname.dirname.sub(Rails.root.join("app", "assets", "templates").to_s, "assets")
# /code/railsapp/app/assets/templates/thing/page.html.haml -> page.html
template[:basename] = pathname.basename(".haml")
# ["/", "assets/thing", "page.html"] -> /assets/thing/page.html
template[:path] = File.join("", template[:dirname], template[:basename])
# Render HAML to HTML
template[:markup] = Haml::Engine.new(pathname.read).render
template_cache << template
end
end
%>
angular.module("app.templates", []).run(["$templateCache", "appName", ($templateCache, appName) ->
templates = [
<% template_cache.each do |template| %>
path: "<%= template[:path] %>"
markup: """
<%= template[:markup] %>
"""
,
<% end %>
]
appPath = "<%= File.join("", "assets", "") %>" + appName
# Only prefill the cache for the current app
$templateCache.put(template.path, template.markup) for template in templates when template.path.indexOf(appPath) == 0
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment