简体   繁体   English

使用翡翠MEAN堆栈,将页面特定的JS放在哪里?

[英]MEAN stack using jade, where to put page specific JS?

I have an app running on the MEAN stack. 我有一个在MEAN堆栈上运行的应用程序。 I am using jade for templates and was wondering where to put page specific javascript. 我正在将jade用于模板,并且想知道在哪里放置页面特定的javascript。 Right now my directory looks like: 现在我的目录如下:

app/
 |- public
 |   |- js
 |   |- css
 |- views
 |- routes
 |- schemas

One of my views, signup.jade , I need to include some javascript: 我的观点之一signup.jade ,我需要包含一些javascript:

$(function() {

    $.validator.addMethod("passwordStrength", function( value, element ) {
        console.log("here")
        var result = this.optional(element) ||
                        /^[a-zA-Z0-9- ]*$/.test(value) &&
                        /\d/.test(value) &&
                        /[a-z]/i.test(value);
        if (!result) {
            var validator = this;
        }
        return result;
    }, "Your password must contain at least one number and one special character.");

    $('#signup').validate({
        rules: {
            email: {
                required: true
            },

            password: {
                required: true,
                passwordStrength: true,
                minlength: 6
            },

            "repeat-password": {
                required: true,
                passwordStrength: true,
                minlength: 6
            }
        }
    });
});

Where is the best place to put this? 放置这个的最佳位置在哪里? Do I create a javascript file for each page inside of app/public/js? 我要为app / public / js内的每个页面创建一个javascript文件吗?

If anyone has any good articles on MEAN file structure best practices as a whole those would be appreciated as well, thanks! 如果有人对MEAN文件结构最佳做法有整体的好的介绍,也将不胜感激,谢谢!

From my experience it is completely fine to keep script in the corresponding jade view if such script is used only once. 根据我的经验,如果只使用一次脚本,将脚本保留在相应的玉器视图中是完全可以的。

You can however create directory with helpers and move this script to this directory (just create a plain js file) and then add it on the page by adding a variable and set its value to the file content. 但是,您可以使用助手创建目录,并将此脚本移动到该目录(只需创建一个普通的js文件),然后通过添加变量将其添加到页面上并将其值设置为文件内容。 It may look a little bit more clean (and allows you to apply js lint to helpers, etc) but requires a bit more work. 它看起来可能更干净(并允许您将js lint应用于助手等),但需要做更多的工作。

在此处输入图片说明 Here is how my code is organized. 这是我的代码的组织方式。 We are using angular fullstack generator by yeoman. 我们正在使用yeoman的角度全栈生成器。 In the image i provided, home.html is the partial view whose controller is home.js. 在我提供的图像中,home.html是其控制器为home.js的部分视图。 I suggest you create a separate file for every html partial page you create. 我建议您为创建的每个html部分页面创建一个单独的文件。 As a matter of fact, a good angular page should have user defined directives, and respective Controllers hooking scope into the directives and the directives managing the scope that has been provided. 实际上,一个好的角度页面应该具有用户定义的指令,并且各个Controller将范围钩接到指令中,并且这些指令管理已提供的范围。 It keeps it neat/simple/beautiful. 它使它保持整洁/简单/美观。 If you get a chance and can afford it, buy the ng-book. 如果您有机会并负担得起,请购买ng-book。 It is beautiful, else even angular's guide is amazing. 它很漂亮,甚至角度指导也很棒。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM