无法在咖啡脚本内渲染咖啡脚本部分

发布于 2024-12-20 00:01:39 字数 1010 浏览 1 评论 0原文

我想制作一个通用控制器,可以在页面加载(使用 AJAX)后调用它,并根据调用它的控制器更新页面上的不同内容。

目的是允许更好的页面和片段缓存,同时为用户显示自定义元素。

我的框架如下所示:

controllers/general_ajax_controller.rb
views/general_ajax/on_page_load.js.coffee
                 ./_update_some_stuff.js.coffee

控制器中的 on_page_load 操作处理确定要加载哪些部分以及视图将呈现部分的逻辑。

on_page_load.js.coffee 视图中,我有这段代码(简化):

<%= render "update_some_stuff" %>

它应该呈现部分。相反,我收到此错误:

  ActionView::Template::Error (SyntaxError: Reserved word "function" on line 2):
  app/views/general_ajax/on_page_load.js.coffee:1:in `_app_views_general_ajax_on_page_load_js_coffee___2304196970850216490_70321203283120'

我认为咖啡脚本是在将其包含在视图中之前编译的(这是咖啡,不支持编译的js)

如果我将 on_page_load 视图的扩展名更改为 .js.erb 然后它可以工作。 ,我必须重新启动我的服务器才能工作,你知道为什么吗?)

你认为这是coffescript中的一个问题还是它是不好的做法,因此不受支持?

(奇怪的是 ,您认为我的动态脚本方法怎么样?

I want to make a general controller that can be called after the page was loaded (with AJAX) and update different stuff on the page depending on which controller called it.

The purpose is to allow better page and fragment caching, while displaying custom elements for the users.

My framework looks like this:

controllers/general_ajax_controller.rb
views/general_ajax/on_page_load.js.coffee
                 ./_update_some_stuff.js.coffee

The on_page_load action in the controller handles the logic of figuring out which partials to load and the view will render the partials.

In the on_page_load.js.coffee view I have this code (simplified):

<%= render "update_some_stuff" %>

which should render the partial. Instead I get this error:

  ActionView::Template::Error (SyntaxError: Reserved word "function" on line 2):
  app/views/general_ajax/on_page_load.js.coffee:1:in `_app_views_general_ajax_on_page_load_js_coffee___2304196970850216490_70321203283120'

I think that the coffeescript is compiled before including it in the view (which is coffee and does not support compiled js)

If I change the extension of the on_page_load view to .js.erb then it works.
(Oddly enough, I have to restart my server before it works, do you know why?)

Do you think this is an issue in coffeescript or is it bad practice and therefore not supported?

As a side discussion, what do you think of my approach to dynamic scrips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-12-27 00:01:39

一种解决方案是将原始解析的 JavaScript 嵌入到视图中。

注意后面的勾号(它告诉coffescript这是原始的javascript)并使用raw()来撤消渲染部分的HTML转义。

`
<%= raw(render("update_some_stuff")) %>
`

One solution is to embed a raw parsed javascript into the view.

Notice the back ticks (which tell coffeescript this is raw javascript) and use of raw() to undo HTML escaping of the rendered partial.

`
<%= raw(render("update_some_stuff")) %>
`
嘿哥们儿 2024-12-27 00:01:39

这是因为 _update_some_stuff.js.coffee 被解释并转换为 Javascript。当它插入到 on_page_load.js.coffee 中时,它会引发错误,因为普通 Javascript 代码在 Coffee-Script 文件内不兼容(在本例中为 function 关键字)。

That's because _update_some_stuff.js.coffee is interpreted and converted to Javascript. When it is inserted into on_page_load.js.coffee it raises an error because vanilla Javascript code is not compatible inside a Coffee-Script file (the function keyword in this case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文