我正在使用django模板和django压缩器来加载我的javascript文件。
base_layout.html:
...
<!-- common imports for all templates -->
{% compress js %}
<script type="text/javascript" src="/static/js/api/jquery-2.2.0.js"></script>
<script type="text/javascript" src="/static/js/api/materialize.js"></script>
<script type="text/javascript" src="/static/js/api/easychem.js"></script>
<script type="text/javascript" src="/static/js/styling/menu_helper.js"></script>
<script type="text/javascript" src="/static/js/api/underscore.js"></script>
<script type="text/javascript" src="/static/js/api/backbone.js"></script>
{% endcompress %}
<!-- custom js imports that each template decides to load -->
{% compress js %}
{% block custom_js %}{% endblock %}
{% endcompress %}
...
compoundReportCard.html
...
{% block custom_js %}
<script type="text/javascript" src="/static/js/styling/scrollspy_helper.js"></script>
<script type="text/javascript" src="/static/js/styling/buttons_helper.js"></script>
<script type="text/javascript" src="/static/js/models/Compound.js"></script>
{% endblock %}
...
Compound.js
var Compound;
Compound = Backbone.Model.extend({});
当我加载页面时,我收到以下错误:
Compound.js:4未捕获的ReferenceError:Backbone不是 定义(匿名函数)@ Compound.js:4
我在在backbone.js
之前加载文件Compound.js之前遇到此错误但如果我这样做:
(请注意,现在我在base_layout.html文件中加载Compound.js文件)
base_layout.html
...
<!-- common imports for all templates -->
{% compress js %}
<script type="text/javascript" src="/static/js/api/jquery-2.2.0.js"></script>
<script type="text/javascript" src="/static/js/api/materialize.js"></script>
<script type="text/javascript" src="/static/js/api/easychem.js"></script>
<script type="text/javascript" src="/static/js/styling/menu_helper.js"></script>
<script type="text/javascript" src="/static/js/api/underscore.js"></script>
<script type="text/javascript" src="/static/js/api/backbone.js"></script>
<!--- note this!!! -->
<script type="text/javascript" src="/static/js/models/Compound.js"></script>
{% endcompress %}
<!-- custom js imports that each template decides to load -->
{% compress js %}
{% block custom_js %}{% endblock %}
{% endcompress %}
...
我没有收到任何错误。
当我使用“custom_js”块时,似乎django模板在第一个场景中以不同的顺序加载javascript文件。我对吗?还是我误解了什么?我该怎么解决这个问题?我想仅为compoundReportCard.html
加载Compound.js文件答案 0 :(得分:0)
我发现错误,我不小心有一个额外的“{%endblock%}”导致了这个问题。