如何在HAML中将逻辑注入到javascript中?
我正在尝试添加 Mercury 编辑器,在修改它的布局时,它建议在视图布局中添加一些 javascript。显然,以下内容不太有效,但它表达了我试图完成的任务的要点。您需要查看 :javascript 过滤器中的部分,在下面我添加 -
来开始 if 语句以了解我的意思:
...
%body{ :class => "#{controller_name} #{action_name}" }
:javascript
var saveUrl = null; // Set to the url that you want to save any given page to.
var options = {
saveStyle: null, // 'form', or 'json' (default json)
saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
visible: null // if the interface should start visible or not (default true)
};
//<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
- if Rails.application.config.respond_to?(:mercury_config)
jQuery.extend(Mercury.config,
= Rails.application.config.mercury_config.to_json.html_safe
);
- end
//<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
- if Rails.application.config.respond_to?(:mercury_page_editor_config)
jQuery.extend(options,
= Rails.application.config.mercury_page_editor_config.to_json.html_safe
);
- end
//<!-- Instantiate the PageEditor -->
new Mercury.PageEditor(saveUrl, options);
...
您能否提供一个示例来说明如何正确地做到这一点?
I'm attempting to add Mercury editor and while modifying the layout for it, it suggests to add some javascript into the view layout. Obviously the following won't quite work, but it expresses the gist of what I'm attempting to accomplish. You'll want to look at the section within the :javascript filter, on down where I add the -
to begin the if statement for what I mean:
...
%body{ :class => "#{controller_name} #{action_name}" }
:javascript
var saveUrl = null; // Set to the url that you want to save any given page to.
var options = {
saveStyle: null, // 'form', or 'json' (default json)
saveMethod: null, // 'POST', or 'PUT', (create, vs. update -- default POST)
visible: null // if the interface should start visible or not (default true)
};
//<!-- Mix in any configurations provided through Rails.application.config.mercury_config -->
- if Rails.application.config.respond_to?(:mercury_config)
jQuery.extend(Mercury.config,
= Rails.application.config.mercury_config.to_json.html_safe
);
- end
//<!-- Mix in any options for PageEditor provided through Rails.application.config.mercury_page_editor_config -->
- if Rails.application.config.respond_to?(:mercury_page_editor_config)
jQuery.extend(options,
= Rails.application.config.mercury_page_editor_config.to_json.html_safe
);
- end
//<!-- Instantiate the PageEditor -->
new Mercury.PageEditor(saveUrl, options);
...
Could you please provide an example of how to do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将逻辑提取到辅助方法中,然后简单地插入它们的结果。看看吧。
You could extract your logic to helper methods and then simply interpolate their results. Take a look.