使用 asset pipeline 和 pjax 时未捕获的 ReferenceError

发布于 2024-12-15 14:33:46 字数 897 浏览 4 评论 0原文

好吧,这听起来像是一个新手错误......但我似乎无法弄清楚发生了什么。 我对 Rails 3.1、coffeescript 和资产管道总体来说还比较陌生,所以我在解决这个问题时遇到了一些麻烦。

无论如何,问题就在这里。

在我的 Users index.html.erb 文件中,我有一个按钮,它将通过 ajax 加载新的用户表单。加载表单的代码位于 users.js.coffee 中,按钮有一个 onClick 方法,引用执行该操作的函数,如下所示。

由于某种原因, index.html.erb

<%= button_tag "New User", :id => "btn_new_user", :onClick => "loadNewUserForm()", :path => "#{new_user_path}" %>

users.js.coffee

loaded = false

loadNewUserForm = ->
  if not loaded
    @path = $('#btn_new_user').attr("path")
    $("#new-users-container").load(@path)
    loaded = true

,当我单击“新用户”按钮时,我不断尝试

 Uncaught ReferenceError: loadNewUserForm is not defined

将函数绑定到 document.ready 中按钮的单击事件(在 users.js.coffee 中声明) )但问题是它没有被调用,因为我正在使用 pjax。

我想我不了解资产管道的工作方式或其他什么...

提前感谢您的帮助。

Ok, this this sounds like a newbie mistake... but I can't seem to figure out what's going on.
I'm relatively new to Rails 3.1, coffeescript and the asset pipeline in general, so I'm having a bit of trouble figuring this one out.

Anyway, here is the problem.

In my Users index.html.erb file I have a button which will load a new user form through ajax. The code to load the form sits in users.js.coffee and the button has an onClick method referring to the function that does that work, like so.

index.html.erb

<%= button_tag "New User", :id => "btn_new_user", :onClick => "loadNewUserForm()", :path => "#{new_user_path}" %>

users.js.coffee

loaded = false

loadNewUserForm = ->
  if not loaded
    @path = $('#btn_new_user').attr("path")
    $("#new-users-container").load(@path)
    loaded = true

for some reason, when I click on the New User button I keep getting

 Uncaught ReferenceError: loadNewUserForm is not defined

I tried binding the function to the click event of the button in the document.ready (declared in users.js.coffee) but the problem with that is that it's not being called because I'm using pjax.

I think I'm not understanding the way asset pipeline works or something...

Thanks in advance for the help.

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

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

发布评论

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

评论(1

给我一枪 2024-12-22 14:33:46

如果有疑问,请转到生成的 JavaScript 以查看发生了什么。您可能会看到这样的内容:

(function() {
  // Your code here
}).call(this);

这是 Javascript 中使用的标准机制,以避免名称空间冲突(Coffeescript 默认情况下这样做),因此对象在该文件之外不可见,除非您显式将它们附加到全局对象(浏览器中的 window,node.js 中的 exports,...)。

顺便说一句,请注意,您没有遵循普遍接受的 Unobtrusive Javascript 准则:您应该设置事件使用 JavaScript,而不是 HTML 标记中的内联事件。一些参考:此处 和 这里

When in doubt go to the generated JavaScript to see what's going on. You'll probably see something like this:

(function() {
  // Your code here
}).call(this);

This is the standard mechanism used in Javascript to avoid namespace collision (Coffeescript does it by default), so objects are not visible outside this file unless you explicitly attach them to a global object (window in the browser, exports in node.js, ...).

By the way, note that you are not following the commonly accepted Unobtrusive Javascript guidelines: you should set events using JavaScript, not with inline events in HTML tags. Some references: here and here.

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