Rails:Asset Pipeline在视图脚本中创建Javascript错误';不打算
我有一个Rails:Asset Pipeline在视图脚本中创建Javascript错误';不打算,javascript,ruby-on-rails,ruby-on-rails-3,asset-pipeline,Javascript,Ruby On Rails,Ruby On Rails 3,Asset Pipeline,我有一个资源的视图和一些特定于它的javascript。因此,我尽职尽责地将Javascript放入app/assets/javascripts/resource.js.coffee 问题是,当我转到应用程序中的其他视图时,会出现各种Javascript错误,如: Type issue: 'null' is not an object... page_url = page_image.getAttribute('src'); 应用程序工作正常,但这些脚本实际上不应该试图在它们不打算在的视图中
资源的视图和一些特定于它的javascript。因此,我尽职尽责地将Javascript放入app/assets/javascripts/resource.js.coffee
问题是,当我转到应用程序中的其他视图时,会出现各种Javascript错误,如:
Type issue: 'null' is not an object...
page_url = page_image.getAttribute('src');
应用程序工作正常,但这些脚本实际上不应该试图在它们不打算在的视图中运行
很明显,我错过了什么……嗯……很明显!它是什么?默认情况下,application.js
包含app/assets/javascripts
中的所有脚本,默认情况下,您的布局包含所有这些脚本。这样做的优点是浏览器只需对Javascript文件发出一个请求,然后就可以缓存这些文件——但缺点是所有Javascript都在每个页面上运行
有两种基本解决方案:
编写Javascript,在需要时执行它应该执行的操作,在不需要时不执行任何操作
将application.js
更改为不包含所有内容,在需要时包含单个Javascript文件,并将config.assets.precompile
更改为包含所有新的顶级文件
默认情况下,application.js
包含app/assets/javascripts
中的所有脚本,默认情况下,您的布局包含所有这些脚本。这样做的优点是浏览器只需对Javascript文件发出一个请求,然后就可以缓存这些文件——但缺点是所有Javascript都在每个页面上运行
有两种基本解决方案:
编写Javascript,在需要时执行它应该执行的操作,在不需要时不执行任何操作
将application.js
更改为不包含所有内容,在需要时包含单个Javascript文件,并将config.assets.precompile
更改为包含所有新的顶级文件
在rails中,没有任何东西只在某些页面上运行一些javascript。我采取的方法就是基于此
我的每个页面特定的coffeescript文件如下所示
window.App.controller_name =
init: ->
#stuff that happens for all actions
edit: ->
#Stuff that happens only for the edit action
body元素由数据控制器和数据动作属性修饰。application.js中的一些javascript然后运行相应的javascript:
execute_hook = (controller, action='init') ->
ns = window.App
if controller?
if ns[controller] && typeof(ns[controller][action]) == "function"
ns[toplevel][controller][action]()
jQuery ->
body = document.body
controller = body.getAttribute("data-controller")
action = body.getAttribute("data-action")
execute_hook(controller)
execute_hook(controller, action)
在rails中,没有任何东西只在某些页面上运行一些javascript。我采取的方法就是基于此
我的每个页面特定的coffeescript文件如下所示
window.App.controller_name =
init: ->
#stuff that happens for all actions
edit: ->
#Stuff that happens only for the edit action
body元素由数据控制器和数据动作属性修饰。application.js中的一些javascript然后运行相应的javascript:
execute_hook = (controller, action='init') ->
ns = window.App
if controller?
if ns[controller] && typeof(ns[controller][action]) == "function"
ns[toplevel][controller][action]()
jQuery ->
body = document.body
controller = body.getAttribute("data-controller")
action = body.getAttribute("data-action")
execute_hook(controller)
execute_hook(controller, action)
有两个建议,第一个是老套的,第二个是很棒的
1.清单+布局
为需要不同JS组合的视图创建新的JS清单文件,从新布局调用该清单文件,并从呈现这些视图的控制器操作调用新布局
在您的示例中:
app/assets/javascripts/resource.js.coffee
// Requires
//= require_whatever_js_files_your_resource_page_needs_here
// Custom JS below
...
app/views/layouts/resource.html.erb
...
<%# Within your <head> tag %>
<%= javascript_include_tag "resource" %>
...
。。。
...
app/controllers/resources\u controller.rb
class ResourceController < ApplicationController
# filters, default layout, other actions, etc.
...
# Your action that needs only the resource JS,
# and therefore the resource layout
def action_that_only_uses_resource_js
@some_objects = SomeObjects.all
render layout: 'resource' # <- This is the important part
end
class ResourceController 渲染布局:“资源”#两个建议,第一个是黑客的,第二个是可怕的
1.清单+布局
为需要不同JS组合的视图创建新的JS清单文件,从新布局调用该清单文件,并从呈现这些视图的控制器操作调用新布局
在您的示例中:
app/assets/javascripts/resource.js.coffee
// Requires
//= require_whatever_js_files_your_resource_page_needs_here
// Custom JS below
...
app/views/layouts/resource.html.erb
...
<%# Within your <head> tag %>
<%= javascript_include_tag "resource" %>
...
。。。
...
app/controllers/resources\u controller.rb
class ResourceController < ApplicationController
# filters, default layout, other actions, etc.
...
# Your action that needs only the resource JS,
# and therefore the resource layout
def action_that_only_uses_resource_js
@some_objects = SomeObjects.all
render layout: 'resource' # <- This is the important part
end
class ResourceController 渲染布局:“资源”#我了解资产管道,所以不打算这样做#2。剩下的#1听起来很合理……这就是我提出问题的原因。有没有关于如何将脚本限制为仅在某些视图中运行的示例?我猜有一个模式,但我还没有找到它……我了解资产管道,所以不打算这样做。剩下的#1听起来很合理……这就是我提出问题的原因。有没有关于如何将脚本限制为仅在某些视图中运行的示例?我猜有一个模式,但我还没有找到它。。。