admin管理员组文章数量:1334670
I'm learning how to develop a node application. It's an app where people can post events happening around the city.
I have an ejs file, new.ejs, that allows users to submit a new event. Obviously there is an event start time and end time. I want to make sure that the end time is AFTER the start time, so I simply added a script to do that, as follows:
<!-- EVENT DATE AND TIME -->
<div class=row>
<!-- DATE -->
<div class="form-group col-md-4">
<label for="date">Date *</label>
<input name="date" type="date" class="form-control" id="date">
</div>
<!--START TIME -->
<div class="form-group col-md-4 ">
<label for="starttime">Start Time *</label>
<input name="starttime" type="text" class="form-control" id="starttime">
</div>
<!--END TIME -->
<div class="form-group col-md-4 ">
<label for="endtime">End Time *</label>
<input name="endtime" type="text" class="form-control" id="endtime">
</div>
<script type="text/javascript">
$('#starttime').timepicker();
$('#endtime').timepicker({
'minTime': '12:00am',
'showDuration': true
});
$('#starttime').on('changeTime', function() {
$('#endtime').timepicker('option', 'minTime', $(this).val());
});
</script>
</div> <!-- END OF ROW -->
Now this works just fine, it does what I want it to do.
However, I know that EJS is designed to take back-end (node) javascript code and render it out into the view.
My question is:
Is adding front-end code between tags a hack? I.e, is this proper coding practice? If not, what is a better way of doing this?
Right now, I just have a small amount of code between my script tags. As I continue to develop the application, what happens if the code gets way too long? Should it remain in the ejs file? Seems too messy...
I'm learning how to develop a node application. It's an app where people can post events happening around the city.
I have an ejs file, new.ejs, that allows users to submit a new event. Obviously there is an event start time and end time. I want to make sure that the end time is AFTER the start time, so I simply added a script to do that, as follows:
<!-- EVENT DATE AND TIME -->
<div class=row>
<!-- DATE -->
<div class="form-group col-md-4">
<label for="date">Date *</label>
<input name="date" type="date" class="form-control" id="date">
</div>
<!--START TIME -->
<div class="form-group col-md-4 ">
<label for="starttime">Start Time *</label>
<input name="starttime" type="text" class="form-control" id="starttime">
</div>
<!--END TIME -->
<div class="form-group col-md-4 ">
<label for="endtime">End Time *</label>
<input name="endtime" type="text" class="form-control" id="endtime">
</div>
<script type="text/javascript">
$('#starttime').timepicker();
$('#endtime').timepicker({
'minTime': '12:00am',
'showDuration': true
});
$('#starttime').on('changeTime', function() {
$('#endtime').timepicker('option', 'minTime', $(this).val());
});
</script>
</div> <!-- END OF ROW -->
Now this works just fine, it does what I want it to do.
However, I know that EJS is designed to take back-end (node) javascript code and render it out into the view.
My question is:
Is adding front-end code between tags a hack? I.e, is this proper coding practice? If not, what is a better way of doing this?
Right now, I just have a small amount of code between my script tags. As I continue to develop the application, what happens if the code gets way too long? Should it remain in the ejs file? Seems too messy...
1 Answer
Reset to default 3If your app is server-side rendered (not a Single Page Application), then your approach is sound.
You can put the your code in JS files in a
/public
folder and configured your node server to serve those files as static files, and add<script>
tags in the.ejs
files that reference those JS files.
I have a sample project with such an implementation. This will be your ejs file (jade for my case) and this is where you put your script files. Lastly, configure your app to serve static assets from a directory like this.
本文标签: javascriptShould I be using script tags in ejs filesStack Overflow
版权声明:本文标题:javascript - Should I be using script tags in ejs files? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374810a2462949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论