简体   繁体   English

如何在Rails 3.1资产管道中正确使用jQuery?

[英]How to properly work with jQuery in Rails 3.1 asset pipeline?

I'm working on a hobby app and using some jQuery. 我正在开发一个爱好应用程序并使用一些jQuery。 The results are fine at the moment, but I'm a jQuery noob and I assume that there are some significant improvements that I can make to the code structure. 结果很好,但我是一个jQuery noob,我认为我可以对代码结构做一些重大改进。 Putting aside Coffescript for the moment, one thing that I've been wondering is how to properly use the model-specific .js files in the asset pipeline. 暂且不考虑Coffescript,我一直想知道的一件事是如何在资产管道中正确使用特定于模型的.js文件。

For example, when working with my User model, I may have some code that I want to have run when the document is ready. 例如,在使用我的用户模型时,我可能会在文档准备好时运行一些我想要运行的代码。 Let's say I put that in $(document).ready(function() {...}); 假设我把它放在$(document).ready(function() {...}); in the users.js file generated by Rails 3.1. 在Rails 3.1生成的users.js文件中。

The next day, I'm working with the Pet model and I have code that I want to run with the document is ready. 第二天,我正在使用Pet模型,我想要在文档准备好的情况下运行代码。 I put that in another $(document).ready(function() {...}); 我把它放在另一个$(document).ready(function() {...}); inside of the pets.js file that Rails prepares. 在Rails准备的pets.js文件中。

Here's where my questions arise: 这是我的问题出现的地方:

  1. How does that compile when the app runs? 应用程序运行时如何编译?
  2. Am I instantiating two jQuery instances with the example above? 我用上面的例子实例化了两个jQuery实例吗?
  3. Should I only use $(document).ready(function() {...}); 我应该只使用$(document).ready(function() {...}); once in the app or does Rails compile my code into a single call? 一旦进入应用程序或Rails将我的代码编译成一个单独的调用?
  4. What belongs in the model-specific .js files? 什么属于特定于模型的.js文件?
  5. Are there differences between how it will execute in development and production modes? 它在开发和生产模式中的执行方式是否存在差异?

1) Compilation: Rails assets pipeline just combines all javascript files in one big file. 1)编译:Rails资产管道只是将所有javascript文件合并到一个大文件中。

2) jquery is only loaded once, you are having multiple $(document).ready functions, but that is not a problem 2)jquery只加载一次,你有多个$(document).ready函数,但这不是问题

3) Rails does not do anything with the call, and jQuery can safely handle more of those blocks per page. 3)Rails对调用没有任何作用,jQuery可以安全地处理每页的更多块。

4) You call it a model-specific .js , I would rather call it controller-specific. 4)你称之为模型特定的.js ,我宁愿称之为控制器特定的。 You group functionality together that belongs together. 您将功能组合在一起。 Whether the thing that ties them together is a controller or a model really does not matter. 将它们联系在一起的东西是控制器还是模型真的无关紧要。 We split our js up in separate files to make it more manageable. 我们将js拆分为单独的文件,以使其更易于管理。

5) In development the assets are compiled on every request, in production it is done only once. 5)在开发过程中,资产是根据每个请求编译的,在生产中它只进行一次。 Also in production it could be minified and compressed. 同样在生产中,它可以缩小和压缩。

Hope this helps. 希望这可以帮助。

I'll attempt to answer some of these for you. 我会尝试为你回答其中的一些问题。 You only really want 1 document ready call per page, but it doesn't matter if you have multiples. 你真的只想要每页1个文件准备好的电话,但是如果你有倍数并不重要。 Whatever is contained within them is going to be the code that executes once the DOM has been loaded into the browser. 它们中包含的内容将是DOM加载到浏览器后执行的代码。 Rails won't do anything magical with the javascript, it will stay the same as what you write in your files. Rails不会对javascript做任何神奇的事情,它会和你在文件中写的一样。 Rails won't compile the javascript code in a funky way, for production environments it may minify it, but the actual code will remain mostly the same. Rails不会以一种时髦的方式编译javascript代码,对于生产环境,它可能会缩小它,但实际代码将保持大致相同。 This is executed by the browser - not the server. 这是由浏览器执行的 - 而不是服务器。

You are not instantiating 2 instances as jQuery is only loaded once, then referenced. 您没有实例化2个实例,因为jQuery只加载一次,然后被引用。 The $(document).ready() call is just a function essentially, nothing more. $(document).ready()调用本质上只是一个函数,仅此而已。

The model-specific jquery files can be used in conjunction with Ajax in a rails app. 特定于模型的jquery文件可以在rails应用程序中与Ajax结合使用。 So you can have files like 'create.js.erb' which is actually a javascript file you can pass rails actions into. 所以你可以拥有像'create.js.erb'这样的文件,它实际上是一个你可以将rails操作传递到的javascript文件。 If you want to fire some specific code after a create/delete post then you can use files like this to do that, you just have to respond the javascript in your rails controller - but this is going a bit deeper that you mean to by the looks of your question above. 如果你想在创建/删除帖子之后触发一些特定的代码,那么你可以使用这样的文件来做到这一点,你只需要在你的rails控制器中响应javascript - 但是你的意思是看看你上面的问题。

The main thing to remember is jquery is just javascript and javascript gets run by the browser - on the front end without any dynamic integration, is will always run on the client side and jquery is fantastic mainly for DOM manipulation. 要记住的主要事情是jquery只是javascript和javascript由浏览器运行 - 在前端没有任何动态集成,总是在客户端运行,jquery主要用于DOM操作。

Hopefully that will clear some stuff up! 希望这会清除一些东西!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM