【问题标题】:Sails JS with EJS but linker to compile handlebars public templatesSails JS 与 EJS 但链接器编译车把公共模板
【发布时间】:2014-02-22 08:45:45
【问题描述】:

我对 Node 和 javascript 后端框架有点陌生,所以请多多包涵:

我一直在寻找前端、后端 (MVC) 框架的完美组合来与 Node 一起工作,我目前决定使用 SailsJS/EmberJS 来制作我可以玩的样板围绕并可能用于未来的项目。

SailsJs(开箱即用生成的应用程序)使用 EJS 编译后端视图。 EmberJs(默认情况下,入门工具包)使用把手来编译前端视图。

我想保持模板语言 (EJS) 不变,但 SailsJS 的链接器有一个例外。它目前将公共模板编译为与车把不兼容的“jst.js”。我想更改此设置,因此“jst.js”将包含车把编译模板,因此它们可以提供给前端(ember 应用程序)以供使用。

我假设为此需要一个额外的节点库。我将如何配置 Gruntfile.js 以使用该库,以便链接器将把手编译的模板输出到公共目录?

【问题讨论】:

    标签: node.js templates ember.js sails.js handlebars.js


    【解决方案1】:

    Sails 中的 EJS 模板与 Ember 的模板关系不大。 Ember 和其他客户端应用程序框架的关键方面之一是如何在客户端而不是在服务器上完成渲染。除了使用 Sails EJS 模板将初始有效负载交付给用户; Sails 最适合作为 REST API 使用 Ember。至于如何处理预编译车把模板以优化初始有效载荷,您可以看看Ember App Kit 是如何实现的。事实上,EAK 是一个很好的起点,原因有很多。

    【讨论】:

      【解决方案2】:

      对于当前版本的 grunt-ember-templates,dev: 部分无法正常工作。 一旦我把它剥离出来,它就立即起作用了:

      var pipeline = require('../pipeline');
      
      module.exports = function(grunt) {
      
      grunt.config.set('emberTemplates', {
              compile: {
                  options: {
                      amd: true,
                      templateBasePath: pipeline.templateBasePath
                  },
                  files: {
                      '.tmp/public/jst.js': pipeline.templateFilesToInject
                  }
              }
      });
      
      grunt.loadNpmTasks('grunt-ember-templates');
      };
      

      【讨论】:

        【解决方案3】:

        您现在可能已经想通了,但要记录在案……

        查看 https://github.com/dgeb/grunt-ember-templates 以获取“附加节点库”

        并查看http://sailsjs.org/#/documentation/concepts/Assets/TaskAutomation.html?q=task-configuration 了解如何配置任务。

        我让它工作的方式是在tasks\config\emberTemplates创建以下文件

        var pipeline = require('../pipeline');
        
        module.exports = function(grunt) {
        
            grunt.config.set('emberTemplates', {
                dev: {
                    compile: {
                        options: {
                            amd: true,
                            templateBasePath: pipeline.templateBasePath
                        },
                        files: {
                            '.tmp/public/jst.js': pipeline.templateFilesToInject
                        }
                    }
                }
            });
        
            grunt.loadNpmTasks('grunt-ember-templates');
        };
        

        并用这些行修改tasks\pipeline.js

        var templateBasePath = 'assets/templates/';
        var templateFilesToInject = [
          templateBasePath + '**/*.hbs' //Note that whatever is replaced by '**/' will be included in the template name (necessary for defining components see http://emberjs.com/guides/components/defining-a-component/).
        ];
        
        module.exports.templateFilesToInject = templateFilesToInject;
        module.exports.templateBasePath = templateBasePath;
        

        当然也可以将您的模板放在assets/templates 中,并带有.hbs 扩展名。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多