[英]Why coffee files in app/assets/javascripts are loaded in all the files?
I have multiple files in app/assets/javascripts, and to test, I have written in a file in this folder : test.coffee : 我在app / assets / javascripts中有多个文件,为了测试,我已经在以下文件夹中写入了一个文件:test.coffee:
$ ->
$(document).ready ->
alert("Hey");
My question is as follows: 我的问题如下:
Why the javascript it loaded in all the pages I visit on my website, I know there are included in application.js but I think it is not optimized, no ? 为什么将javascript加载到我访问我的网站的所有页面中,我知道其中包含application.js,但我认为它没有经过优化,没有吗?
What do you think about that ? 您如何看待? Could you tell me an optimized method to make ajax calls in multiple pages but where are not included in all pages of the website ?
您能告诉我一种优化的方法来在多个页面中进行Ajax调用,但是网站的所有页面中没有包含吗? Actually I do like that :
其实我喜欢这样:
<script type="text/javascript">
$(document).on('keyup', 'input#field_seller_name', function(e){
if(e.keyCode == 13 && $(this).val() != ""){
$.ajax({
url: '/field_sellers/' + $(this).parent().parent().data('field_seller_id'),
type: 'PUT',
dataType: 'script',
data: {
field_seller: {
name: $(this).val()
}
},
error: function(result){
alert(result.responseText);
}
});
}
});
</script>
I put this part of code in a file and it is loaded only in one page put I think it is not clean... Otherwise I do like that : 我将这部分代码放在一个文件中,并且只在一页中加载,我认为它不干净...否则我会这样:
$ ->
# This is for CREATE an new type_material by press enter touch with input in focus
$(document).on 'keyup', '#name_new_type_material', (evt) ->
if evt.keyCode == 13
$.ajax '/type_materials',
type: 'POST',
dataType: 'script',
data: {
field_type_user: {
name: $(this).val()
}
}
But like I have said on the top, it is loaded in all the website ... 但是就像我在顶部说的那样,它已加载到所有网站中...
Linguee helped me to write this question... Linguee帮助我写了这个问题...
Thanks ! 谢谢 !
Why coffee files in app/assets/javascripts are loaded in all the files?
为什么将app / assets / javascripts中的coffee文件加载到所有文件中?
That is by design. 那是设计使然。 Your
views/layouts/application.html.erb
loads javascript file application.js
. 您的
views/layouts/application.html.erb
会加载javascript文件application.js
。 If you go to app/assets/javascripts/application.js
, you'll see instructions to load all of your assets. 如果您转到
app/assets/javascripts/application.js
,则会看到有关加载所有资产的说明。 Since application.js
is loaded on all of your pages (due to it being in the layout template), all your assets are loaded on all pages. 由于
application.js
已加载到您的所有页面上(由于它位于布局模板中),因此所有资产都加载到了所有页面上。
What you can do is create another "master" file. 您可以做的是创建另一个“主”文件。 Say,
app/assets/javascripts/application-with-custom-ajax.js
and load your additional logic in it (and not load it in application.js
). 说,
app/assets/javascripts/application-with-custom-ajax.js
并在其中加载其他逻辑(而不是在application.js
加载)。 Then you just have to make sure to include this alternate file instead of application.js
on pages where you need the additional logic. 然后,您只需要确保在需要其他逻辑的页面上包括此替代文件,而不是
application.js
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.