2014-01-15 48 views
1

我用咕噜-烬模板成功预编译车把成一个文件,但是,包括在HTML文件源时:包括预编译的余烬车把模板

<script src="templates/app/compiledTemplates.js" type="text/javascript"></script> 

它这样说:

Resource interpreted as Script but transferred with MIME type text/plain: "file:///Users/jaime/voyant-svn/voyant-blocks/dev/blocks-web/src/main/webapp/templates/app/compiledTemplates.devjs". 

包含预编译的车把模板的正确方法是什么?

+0

你确定要将模板编译到模板目录吗?我通常会将汇编后的代码放到一个“dist”文件夹中,这样它就不会混淆我的项目中可能存在的其他任何实际源代码。 –

回答

1

我结合编译模板与我的其他脚本的指针(jQuery的/ ember/ember-data /我的应用程序代码)使用grunt。然后,在我的index.html中,我只包含单个js脚本(也有助于减少http请求的数量)。

我目前正在使用grunt这个,一个简单的“构建”步骤可能看起来像下面。要使用这个,你需要安装nodejs。接下来,您将NPM安装以下

grunt 
grunt-cli 
grunt-ember-template-compiler 
grunt-contrib-concat 

一旦你已经安装了这些,你可以从你的根,如果你把JS到一个名为“Gruntfile.js”文件 - 然后简单地执行“咕噜”的运行和构建它会输出一个deps.min.js(带你的脚本和模板组合)

module.exports = function(grunt) { 
    grunt.loadNpmTasks('grunt-contrib-concat'); 
    grunt.loadNpmTasks('grunt-ember-template-compiler'); 

    grunt.initConfig({ 
    concat: { 
     dist: { 
      src: [ 
      'website/static/website/script/vendor/handlebars-v1.2.1.js', 
      'website/static/website/script/vendor/ember.min.js', 
      'website/static/website/script/my-app.js’, 
      'website/static/website/script/dist/tmpl.min.js'], 
      dest: 'website/static/website/script/dist/deps.min.js' 
     } 
    }, 
    emberhandlebars: { 
     compile: { 
      options: { 
       templateName: function(sourceFile) { 
        var newSource = sourceFile.replace('website/static/website/script/app/templates/', ''); 
        return newSource.replace('.handlebars', ''); 
       } 
      }, 
      files: ['website/static/website/script/app/templates/**/*.handlebars'], 
      dest: 'website/static/website/script/dist/tmpl.min.js' 
     } 
    } 
    }); 

    grunt.task.registerTask('default', ['emberhandlebars', 'concat:dist']); 
}; 
1

这是我要做的事在我的应用程序:

<script type="text/javascript" charset="utf-8" src="dist/templates.js"></script> 

在这里可以看到整个index.html文件:

https://github.com/joachimhs/WarmCroc-Ember/blob/master/index.html

在,其实,我只是写了这个代码今天在Ember.js上进行现场编码演示。谈话记录为一个截屏,并且可从http://emberjstraining.com

这个谈判应该给你你需要把一切都设置正确:)

+0

我一定会看这个演讲,它似乎有用的信息。 – Jaime

+0

我似乎在编译与您非常相似的模板。除了唯一的区别是我的模板文件在文件中包含脚本标记(