在使用咖啡脚本定义被忽略:远程=>真实js.erb,响应脚本、定义、咖啡、真实

2023-09-10 21:24:40 作者:蘇辭

好了,考虑以下因素: A 应用程序/视图/页/ index.html.erb ,其中包括:

Okay, consider the following: A app/views/pages/index.html.erb, consisting of:

<div id='content'>    
  <%= link_to 'Test', get_page_path('test'), :remote => true %>
</div>

在定义应用程序/控制器/ pages_controller.rb 是这样的:

def get
  @page = Page.new(:title => '404', :content => '<strong>Page not found</strong>') unless @page = Page.find_by_title(params[:title])
  respond_to do |format|
    format.html
    format.js
  end
end

JavaScript的资产在应用程序/资产/ Java脚本/ pages.js.coffee 是这样的:

bork = (swedish) -> alert swedish
$ ->
  bork 'bork bork'

最后一个应用程序/视图/页/ get.js.coffee 是这样的:

bork 'Smoerrebroed'
$('#content').html '<%= escape_javascript(@page.content).html_safe %>'

所以,这里是有趣的部分 - 我的确看到了博克博克警报一旦如预期。然而,点击链接后,只有内容被替换(由第二线所示),我没有看到一个Smoerrebroed警报。 该 pages.js.coffee 的东西显然是包括 - 但似乎是在页面本身之外无法访问。因此,条块分割的Rails做的还非常有用的 - 到底为什么要我把我的网页控制具体的东西到 pages.js.coffee 时,它是唯一能够里面具体的脚本?有没有什么办法来解决呢? 这似乎是基于这样的咖啡脚本包里面的东西一个事实

So, here's the fun part - I do see the 'bork bork' alert once as expected. However, after clicking on the link, only the content gets replaced (as indicated by the second line), I don't see a 'Smoerrebroed' alert. The pages.js.coffee stuff is obviously included - but seems to be inaccessible outside the page itself. As such, the compartmentalization Rails is doing is not very useful - why exactly should I put my 'pages controller' specific stuff into pages.js.coffee when it is only accessible inside that specific script? Is there any way to work around that? It seems to based on the fact that coffee script wraps everything inside a

(function() {
  //specific stuff here
}).call(this);

这理应是那里prevent从执行的页面加载前的脚本。有没有告诉CoffeeScript的一些办法阻止这样做,因为我使用了 $ - &GT; (简写)或者 $(文件)。就绪()函数反正...

which supposedly is there to prevent the script from executing before the page has loaded. Is there some way of telling coffeescript to stop doing that because I'm using the $ ->(shorthand) or $(document).ready() function anyway...

推荐答案

您说,CoffeeScript的函数包装

You say that the CoffeeScript function wrapper

据说是那里prevent从执行的页面加载前的脚本。

supposedly is there to prevent the script from executing before the page has loaded.

这实际上是第二次今天这个误解已经上来了,所以我会指示你我刚才的答复对这个问题Coffeescript,骨干网和加载顺序。

That's actually the second time today this misconception has come up, so I'll point you to my earlier answer on the question Coffeescript, Backbone and load order.

要解决你的问题的主要部分:是的,有可能关闭包装(全球范围),但绝对不推荐。请参阅How我可以使用选项&QUOT; - 裸&QUOT;在Rails的3.1 CoffeeScript的?

To address the main part of your question: Yes, it is possible to turn off the wrapper (globally), but certainly not recommended. See How can I use option "--bare" in Rails 3.1 for CoffeeScript?

你应该做的是让博克全局函数。无论是将其附加到窗口或作为速记,使用 @ (因为这个 / @ 指向窗口在外部环境),即

What you should do instead is make bork a global function. Either attach it to window or, as a shorthand, use @ (since this/@ points to window in the outer context), i.e.

@bork = (swedish) -> alert swedish

了解看看函数包装是一种福气,不是祸,这意味着,如果你的团队恰好在两个不同的文件中定义的东西叫做博克,他们赢得了牛逼互相覆盖,除非他们都明确地做出全球性的。这不只是一个CoffeeScript的主义;使用函数包装器的每个文件,以避免污染全局命名空间被认为是一个很好的JavaScript的做法。 jQuery的,比如,是否在其每个几个组件模块的。

Learn to see the function wrapper as a blessing, not a curse—it means that if your team happens to define things called bork in two different files, they won't overwrite each other, unless they're both explicitly made global. It's not just a CoffeeScript-ism; using a function wrapper around each file to avoid polluting the global namespace is considered a good JavaScript practice. jQuery, for instance, does it in each of its several component modules.