Module: RequirejsHelper
- Defined in:
- app/helpers/requirejs_helper.rb
Constant Summary collapse
- @@_priority =
[]
Instance Method Summary collapse
- #_almond_include_tag(name, &block) ⇒ Object
- #_javascript_path(name) ⇒ Object
- #_once_guard ⇒ Object
- #_requirejs_data(name, &block) ⇒ Object
- #base_url(js_asset) ⇒ Object
- #requirejs_include_tag(name = nil, &block) ⇒ Object
Instance Method Details
#_almond_include_tag(name, &block) ⇒ Object
86 87 88 |
# File 'app/helpers/requirejs_helper.rb', line 86 def _almond_include_tag(name, &block) "<script src='#{_javascript_path name}'></script>\n".html_safe end |
#_javascript_path(name) ⇒ Object
90 91 92 93 94 95 96 |
# File 'app/helpers/requirejs_helper.rb', line 90 def _javascript_path(name) if defined?(javascript_path) javascript_path(name) else "/assets/#{name}" end end |
#_once_guard ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'app/helpers/requirejs_helper.rb', line 75 def _once_guard if defined?(controller) && controller.requirejs_included raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page." end retval = yield controller.requirejs_included = true if defined?(controller) retval end |
#_requirejs_data(name, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/helpers/requirejs_helper.rb', line 10 def _requirejs_data(name, &block) {}.tap do |data| if name name += ".js" unless name =~ /\.js$/ data['main'] = _javascript_path(name). sub(/\.js$/,''). sub(base_url(name), ''). sub(/\A\//, '') end data.merge!(yield controller) if block_given? end.map do |k, v| %Q{data-#{k}="#{v}"} end.join(" ") end |
#base_url(js_asset) ⇒ Object
98 99 100 101 102 103 |
# File 'app/helpers/requirejs_helper.rb', line 98 def base_url(js_asset) js_asset_path = javascript_path(js_asset) uri = URI.parse(js_asset_path) asset_host = uri.host && js_asset_path.sub(uri.request_uri, '') [asset_host, Rails.application.config.relative_url_root, Rails.application.config.assets.prefix].join end |
#requirejs_include_tag(name = nil, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/helpers/requirejs_helper.rb', line 26 def requirejs_include_tag(name=nil, &block) requirejs = Rails.application.config.requirejs if requirejs.loader == :almond name = requirejs.module_name_for(requirejs.build_config['modules'][0]) return _almond_include_tag(name, &block) end html = "" _once_guard do unless requirejs.run_config.empty? run_config = requirejs.run_config.dup unless _priority.empty? run_config = run_config.dup run_config[:priority] ||= [] run_config[:priority].concat _priority end if Rails.application.config.assets.digest modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m } # Generate digestified paths from the modules spec paths = {} modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/,'' } if run_config.has_key? 'paths' # Add paths for assets specified by full URL (on a CDN) run_config['paths'].each { |k,v| paths[k] = v if v =~ /^https?:/ } end # Override user paths, whose mappings are only relevant in dev mode # and in the build_config. run_config['paths'] = paths end run_config['baseUrl'] = base_url(name) html.concat " <script>var require = \#{run_config.to_json};</script>\n HTML\n end\n\n html.concat <<-HTML\n <script \#{_requirejs_data(name, &block)} src=\"\#{_javascript_path 'require.js'}\"></script>\n HTML\n\n html.html_safe\n end\nend\n" |