English 中文(简体)
如何将CoffeeScript文件与视图关联?
原标题:How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline By default all coffeescript is compiled into a single application.js file, this is a good thing.

Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...

Many of these scripts will be specific to a controller or action, I am trying to grok the conventional way to handle this, since everything is wrapped in an anonymous function how do I only execute just the code for a particular controller / action, by default all of the anonymous functions are being executed

I did play around with some hacks where I load the controller and action name into js variables and then in coffeescript check those to conditionally run code, I don t like that very much

my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view, going to spike this using the default_bare = true configuration

请参阅如何在Rails3.1中为CoffeeScript使用选项”--bare“

编辑

环顾四周:这看起来可能是正确的方法-Rails 3.1和Coffeescription出现“找不到变量”错误

最佳回答

有两种常见的方法:

  1. 使行为以特定元素的存在为条件。例如,运行注册表的代码应该在前面加上以下内容

    if$(#signup).length>;0

  2. 使行为以body元素上的类为条件。可以使用ERB设置实体类。这通常也是样式表所需要的。代码应该是这样的

    if$(body).hasClass用户

问题回答

gistyle是一个简单的gem,可以帮助您运行特定于操作的javascript代码。

按照它的设置,可以在身体元素中设置一些数据属性,表示当前控制器和动作名称。然后,它只会在加载相应的视图时调用该操作。





相关问题
How do I define global variables in CoffeeScript?

On Coffeescript.org: bawbag = (x, y) -> z = (x * y) bawbag(5, 10) would compile to: var bawbag; bawbag = function(x, y) { var z; return (z = (x * y)); }; bawbag(5, 10); compiling via ...

I m trying to re-write this in CoffeeScript. Coming unstuck

function getElementsByClassName(className) { // get all elements in the document if (document.all) { var allElements = document.all; } else { var allElements = document.getElementsByTagName("...

Anonymous functions syntax in CoffeeScript

I ve been looking at CoffeeScript and I m not understanding how you would write code like this. How does it handle nested anonymous functions in its syntax? ;(function($) { var app = $....

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

Custom compiler with maven

I m trying to make Maven2 to compile coffeescript to javascript. As far as I m concerned there is no plugin which provides compiling coffeescript. Is there a compiler-plugin for maven which can be ...

what is wrong when I compile my .coffee files

Hi there Iam using coffeeScript for my apps now and I love it but recently I ve been having a lot of trouble with compilation, Iam using it for a rails application and when I run coffee -w -c public/...

CoffeeScript on Windows?

How can I try CoffeeScript on Windows? The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation EDIT: Since I asked this a while ago, many new ...

How can I compile CoffeeScript from .NET?

I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don t want to use [Rhino][...

热门标签