在文件夹中组织ember模板

时间:2013-09-21 02:40:18

标签: ember.js

我有这样的路由器地图:

this.resource('eng', function(){
    this.route('home');
    this.resource('eng.rent', {path: 'rent' }, function(){
        this.route('boulderSmall', {path: 'boulder-small'});
        this.route('boulderXl', {path: 'boulder-xl'});
    });     
});

模板文件存储在" templates / eng"夹;对于"家庭"和" eng.rent"路线一切正常:Ember可以自己找到模板文件的位置;但对于其他路线,我必须指定模板的位置,例如:

Importclimbing.EngRentBoulderSmallRoute = Importclimbing.StdEngRoute.extend({
    renderTemplate: function() {
        this.render('eng/boulderSmall');
    }
});

有人可以解释Ember如何查找模板文件吗?例如,如果我没有指定" renderTemplate"对于EngRentBoulderSmallRoute,如上所述,模板将不会渲染(即使我将" boulderSmall.hbs"文件放入"模板"文件夹而不是" template / eng&#34 ;;所以,默认情况下Ember会查找这个模板吗?如果我想将&b 34SarsSmall.hbs"存储到" templates / eng / rent"文件夹,我应该将哪条路径传递给renderTemplate函数?

2 个答案:

答案 0 :(得分:5)

您的文件夹结构应如下所示。

首先,您需要重命名eng.rent路由rent,以便路由器看起来像这样

this.resource('eng', function(){
    this.route('home');
    this.resource('rent', {path: 'rent' }, function(){
        this.route('boulderSmall', {path: 'boulder-small'});
        this.route('boulderXl', {path: 'boulder-xl'});
    });     
});

然后你的模板和文件夹应该这样命名。

templates          ## this is a folder
  |--eng           ## this is a folder
      |--home.hbs
  |--rent      ## this is a folder
      |--boulder_small.hbs
      |--boulder_xl.hbs
  application.hbs
  eng.hbs
  rent.hbs

我希望这会有所帮助。 干杯

答案 1 :(得分:3)

最后我摆脱了这个; kiwiupower的答案是正确的,因为Ember在文件夹和子文件夹中查找模板文件,如图所示; 问题是由于自然,我用于发展;在gruntfile中,默认设置仅通过一个级别的文件夹查找Ember模板;

让yeoman能够更深入地查看模板文件夹结构,我做了这个改动:

1为livereload的监视任务:

 emberTemplates: {
            files: '<%= yeoman.app %>/templates/**/**/*.hbs',
            tasks: ['emberTemplates', 'livereload']
        },

我添加了一个“** /”,以便让任务同时监视模板目录中的第二级子文件夹

在Ember模板任务中

2:

 emberTemplates: {
        options: {
            templateName: function (sourceFile) {
                var templatePath = yeomanConfig.app + '/templates/';
                return sourceFile.replace(templatePath, '');
            }
        },
        dist: {
            files: {
                '.tmp/scripts/compiled-templates.js': '<%= yeoman.app %>/templates/{,*/}{,*/}*.hbs'
            }
        }
    }

我在dist.files对象中添加了一个“{, /}”; 如果您需要yeoman来监视/编译第三级子文件夹或更多,您需要修改这两个任务,添加更多“ * /”和“{,* /}”