From 70b266fa009c9cb730acb20f7a1fdb4d19801bd8 Mon Sep 17 00:00:00 2001 From: Jon Wu Date: Wed, 23 Jul 2014 10:04:35 -0700 Subject: [PATCH] Allow defining functions in requirejs.yml Some RequireJS config options such as init need a function, however YAML and JSON can only store data - not JavaScript. As a workaround this detects a function and unescapes it when the config is added to the page in the helper. --- app/helpers/requirejs_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/helpers/requirejs_helper.rb b/app/helpers/requirejs_helper.rb index fdae78c5..b64c3ce9 100644 --- a/app/helpers/requirejs_helper.rb +++ b/app/helpers/requirejs_helper.rb @@ -59,8 +59,12 @@ def requirejs_include_tag(name=nil, &block) end run_config['baseUrl'] = base_url(name) + # Detect functions in JSON and unescape them so they can be evaluated by RequireJS + run_config_json = run_config.to_json.gsub(/"(function\(.*?\)\s*?{.*?}[\s\\n]*)"/) do |f| + eval(f).strip.delete("\n") + end html.concat <<-HTML - + HTML end