简体   繁体   English

Rails 4 / Sprockets:特定于控制器的JavaScript随处可用?

[英]Rails 4 / Sprockets: controller-specific JavaScripts are available everywhere?

I'm running a Rails 4.2.4, and when creating a controller auth , app/assets/javascripts/auth.js is automatically created. 我正在运行Rails 4.2.4,并在创建控制器auth时自动创建app/assets/javascripts/auth.js It tells me that: 它告诉我:

// Place all the behaviors and hooks related to the matching controller here. //在此处放置与匹配控制器相关的所有行为和挂钩。 // All this logic will automatically be available in application.js. //所有这些逻辑将自动在application.js中可用。

That's fine, but since application.js manifest includes a //require_tree . 很好,但是由于application.js清单包含//require_tree . , any file I add in the app/assets/javascripts folder will be available to any controller. ,我添加到app/assets/javascripts文件夹中的任何文件将对任何控制器可用。

I was under the impression that those files were specific to one controller and wouldn't show up anywhere else but on pages of, say, AuthController . 我的印象是,这些文件是特定于一个控制器的,不会在其他地方显示,而是在AuthController页面上AuthController

Am I missing something here or is this by design and simply exists to help organize workflow, but nothing more? 我是否在这里遗漏了一些东西,或者是设计使然,仅仅是为了组织工作流程而已,仅此而已?

You are right in that for the situation you described, any javascript you put in app/assets/javascripts/auth.js will be available to to any views that use your application.js manifest, not just ones rendered by your AuthController . 正确地说,对于您所描述的情况,您放置在app/assets/javascripts/auth.js所有javascript都可用于使用application.js清单的任何视图,而不仅仅是AuthController呈现的AuthController

This is by design and is perhaps misleading. 这是设计使然,可能会引起误解。 How to organize javascript within the Rails framework is a bit of a hot-topic regularly debated and without consistent conventions. 如何在Rails框架内组织javascript是一个经常被争论且没有一致约定的热点话题。

The idea of splitting your javascript into a file/directory structure matching the rest of your application code is just a convenience measure to help you know where to look for or place "sprinkles" of javascript. 将javascript拆分为与其余应用程序代码匹配的文件/目录结构的想法只是一种方便的措施,可以帮助您知道在哪里寻找或放置javascript的地方。 Before Rails 4, view helper modules were in a similar state - their filenames matched respective views and controllers but the methods themselves were available across the entire app (by default). 在Rails 4之前,视图帮助器模块处于类似状态-它们的文件名与相应的视图和控制器匹配,但是方法本身在整个应用程序中都可用(默认情况下)。

That said, just because Rails doesn't impose limitations on javascript scopes, doesn't mean you shouldn't follow a convention and manually enforce it. 就是说,仅仅因为Rails不会对javascript范围施加限制,并不意味着您不应该遵循约定并手动执行约定。 Breaking your application manifest up into multiple controller manifests or putting erb logic inside of your layout can help achieve this. 将您的应用程序清单分解为多个控制器清单或在布局中放入erb逻辑可以帮助实现此目的。

Like I said, it's a hot-topic so if you do a little Googling on how to organize javascript in Rails you'll find tons of blogs with articulated suggestions. 就像我说的那样,这是一个热门话题,因此,如果您对如何在Rails中组织javascript进行一点点谷歌搜索,就会发现大量带有明确建议的博客。 Here's a decent example: 这是一个不错的例子:

http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/ http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/

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

相关问题 如何使用javascript_include_tag从供应商/资产/ javascripts添加控制器特定的javascript? - How to add a controller-specific javascript from vendor/assets/javascripts using javascript_include_tag? 无法在Rails(5+)中加载控制器特定的javascript文件 - Unable to load controller-specific javascript file in Rails (5+) 如何确保在控制器特定的JS之后调用Rails资产管道中的JQuery脚本? - How do I make sure JQuery scripts in Rails asset pipeline are called after controller-specific JS? 在symfony2中管理控制器特定的javascript有哪些选项? - What are the options for managing controller-specific javascript in symfony2? Angular-从另一个控制器调用特定于控制器的函数的最佳实践是什么? - Angular— What is the best practice for calling a controller-specific function from another controller? 如何在文档头中使用AngularJS控制器特定的作用域值? - How can I use AngularJS controller-specific scope values in document head? jQuery无法在Rails中使用Sprockets - Jquery not working with Sprockets in Rails 链轮中的链轮意味着什么 - What sprockets mean in rails Rails 4和控制器特定资产 - Rails 4 and controller specific assets Rails Javascript +资产管道 - Rails Javascripts + Asset Pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM