简体   繁体   English

Sails.js和Angular.js项目结构和非SPA案例的配置

[英]Sails.js and Angular.js project structure and configuration for non-SPA case

I am starting a side-project based in Sails to try it. 我正在开始一个基于Sails的侧面项目来尝试它。 Most of the pages are server-side rendered via EJS and don't require javascript on the front-end (my landing page doesn't, my "about" page certainly doesn't etc). 大多数页面都是通过EJS进行服务器端呈现的,并且前端不需要javascript(我的登陆页面没有,我的“关于”页面肯定没有等)。 However, I have a few pages that have quite a lot client-side functionality and I want to use Angular, because I am mostly familiar with that framework. 但是,我有一些页面具有相当多的客户端功能,我想使用Angular,因为我大多熟悉该框架。 The routing to these pages is again handled in the server and there's really no meaning in bundling them as a SPA. 这些页面的路由再次在服务器中处理,将它们捆绑为SPA实际上没有任何意义。

So I am trying to wrap my mind around these concerns: 因此,我试图围绕这些问题进行思考:

  • Where to place the Angular app's scripts? 在哪里放置Angular应用程序的脚本?
    • Is /assets/js/dependencies still the proper place? /assets/js/dependencies仍然是合适的地方吗? Wouldn't placing them there make the Grunt task inject them in layout.ejs and thus in every page? 不会将它们放在那里使Grunt任务将它们注入layout.ejs并因此在每个页面中?
  • How to conditionally load the Angular base and it's components (controllers, services, etc)? 如何有条件地加载Angular基础及其组件(控制器,服务等)?
    • Sails uses views/layout.ejs as a base layout for loading project-wide styles, templates and scripts. Sails使用views/layout.ejs作为基础布局来加载项目范围的样式,模板和脚本。 Each page's controller handles injecting the body part into this layout according to the view "partial" that has been developed for that page. 每个页面的控制器根据为该页面开发的视图“部分”处理将body部分注入此布局。 Is this view "partial" .ejs file the appropriate place to conditionally load the Angular app files in only the pages that require them? 这个视图是“部分”.ejs文件是否只在需要它们的页面中有条件地加载Angular应用程序文件?
  • How to add min/conctact/uglify of Angular' script sources in Grunt tasking? 如何在Grunt任务中添加Angular'脚本源的min / conctact / uglify?
    • All the Angular related files will need to be concatenated, minified/uglified for production. 所有Angular相关文件都需要连接,缩小/ uglified用于生产。 This will need to be a new js concatenated file to be loaded in appropriate pages apart from the "generic" js file that currently Sails tasks create and is loaded in every page. 这将需要是一个新的js连接文件,除了当前Sails任务创建并在每个页面中加载的“通用”js文件之外,还要加载到适当的页面中。 So we're essentially talking about two concatenated js files for the client side. 所以我们基本上是在谈论客户端的两个连接的js文件。 One that is globally loaded, and the Angular one that only the pages that need it load. 一个是全局加载的,而Angular只加载需要它的页面。 Which parts of the build/tasking procedure will require modifications? 构建/任务过程的哪些部分需要修改? Some examples or resources to check would be highly useful here. 要检查的一些示例或资源在这里非常有用。

Where to place the Angular app's scripts? 在哪里放置Angular应用程序的脚本?

Is /assets/js/dependencies still the proper place? / assets / js / dependencies仍然是合适的地方吗?

No, just put your angular.min.js in your dependencies folder, but not your Angular app's script. 不,只需将angular.min.js放在您的依赖项文件夹中,而不是您的Angular应用程序的脚本。 You need to put all you Angular app in the assets/js folder ( or in a sub-folder, but not in dependencies ) 您需要将所有Angular应用程序放在assets/js文件夹中(或在子文件夹中,但不在依赖项中)

To be sure that each file of your app will be loaded in the right order (for example you need to load first the Js file which inject your angular app's dependencies), you can modify the tasks/pipeline.js file, and specify the order you want : You need to modify the jsFilesToInject array which contains all the Js files to load in the right order. 为了确保您的应用程序的每个文件都以正确的顺序加载(例如,您需要首先加载注入角度应用程序依赖项的Js文件),您可以修改tasks/pipeline.js文件,并指定顺序你想要:你需要修改jsFilesToInject数组,其中包含jsFilesToInject正确的顺序加载的所有Js文件。

For example for your project : 例如,对于您的项目:

var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',
  // loading angularJS
  'js/dependencies/angular.min.js',
  // all the rest in dependencies
  'js/dependencies/**/*.js',
  // loading first AngularModule definition
  'js/app/app.module.js',
  // all the rest of the angular application
   'js/app/**/*.js'
];

For your other question I think you need to look at the tasks/config/sails-linker.js file, that inject all the Js scripts in the <!--SCRIPTS--> tags in your HTML code. 对于您的其他问题,我认为您需要查看tasks/config/sails-linker.js文件,该文件会在HTML代码中的<!--SCRIPTS-->标记中注入所有Js脚本。

I hope that it will help you and that I'm not too late ! 我希望它会帮助你,我也不会太迟!

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

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