Javascript Can';t通过木偶布局视图中的gulp把手访问预编译的把手模板
虽然我在Mac上没有问题,但这个问题是在我将项目克隆到Windows机器上之后发生的。所有的gulp任务运行起来都没有问题,但是我在控制台上遇到了一个“Uncaught UndefinedTemplateError:无法呈现模板,因为它是null或undefined”错误,并且浏览器不会呈现任何html。有一个gulp手柄负责编译hbs文件。当Marionette.LayoutView尝试访问模板时,它返回undefined。 这是我用的样板 这是我的布局图Javascript Can';t通过木偶布局视图中的gulp把手访问预编译的把手模板,javascript,backbone.js,gulp,handlebars.js,marionette,Javascript,Backbone.js,Gulp,Handlebars.js,Marionette,虽然我在Mac上没有问题,但这个问题是在我将项目克隆到Windows机器上之后发生的。所有的gulp任务运行起来都没有问题,但是我在控制台上遇到了一个“Uncaught UndefinedTemplateError:无法呈现模板,因为它是null或undefined”错误,并且浏览器不会呈现任何html。有一个gulp手柄负责编译hbs文件。当Marionette.LayoutView尝试访问模板时,它返回undefined。 这是我用的样板 这是我的布局图 Marionette.LayoutV
Marionette.LayoutView.extend({
el: 'body',
template: templates['root-layout']
config.js
module.exports = {
handlebars: {
src: {
templates: 'app/scripts/**/[^_]*.hbs',
partials: ['app/scripts/**/_*.hbs']
},
dest: 'app/scripts/'
// dest: '.tmp/scripts/'
}
};
handlebar.js
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var config = require('./config').handlebars;
var merge = require('merge-stream');
gulp.task('handlebars', function () {
var partials = gulp.src(config.src.partials)
.pipe($.handlebars())
.pipe($.wrap('Handlebars.registerPartial("<%= processPartialName(file.relative) %>", Handlebars.template(<%= contents %>));', {}, {
imports: {
processPartialName: function (filePath) {
var matches = filePath.match(new RegExp('(modules/(\\w+)/templates|templates)/((.*)\/?)_(.*).js'));
if (!matches) {
return filePath;
}
return (matches[2] ? matches[2] + '/' : '') + (matches[3] ? matches[3] : '') + matches[5];
}
}
}));
// Load templates from the templates/ folder relative to where gulp was executed
var templates = gulp.src(config.src.templates)
// Compile each Handlebars template source file to a template function
.pipe($.handlebars())
.pipe($.wrap('Handlebars.template(<%= contents %>)'))
.pipe($.declare({
namespace: 'templates',
noRedeclare: true, // Avoid duplicate declarations
processName: function (filePath) {
var matches = filePath.match(new RegExp('(modules/(\\w+)/templates|templates)/((.*)\/?)(.*).js'));
alert(matches);
if (!matches) {
return filePath;
}
return (matches[2] ? matches[2] + '/' : '') + (matches[3] ? matches[3] : '');
}
}));
// Output both the partials and the templates as build/js/templates.js
return merge(partials, templates)
.pipe($.concat('templates.js'))
.pipe($.wrap('define(["handlebars"], function(Handlebars) {<%= contents %>return this["templates"];});'))
.pipe(gulp.dest(config.dest));
});
var gulp=require('gulp');
var$=require('gulp-load-plugins')();
var config=require('./config')。把手;
var merge=require('merge-stream');
狼吞虎咽任务('把手',功能(){
var partials=gulp.src(config.src.partials)
.pipe($.handlebar())
.pipe($.wrap('handlebar.registerPartial(“,handlebar.template());”,{}{
进口:{
processPartialName:函数(文件路径){
var matches=filePath.match(新的RegExp('(模块/(\\w+)/templates |模板)/((.*)\/?)\u(.*.js');
如果(!匹配){
返回文件路径;
}
返回(匹配[2]?匹配[2]+'/':'')+(匹配[3]?匹配[3]:'')+匹配[5];
}
}
}));
//从相对于执行gulp的位置的模板/文件夹加载模板
var templates=gulp.src(config.src.templates)
//将每个把手模板源文件编译为模板函数
.pipe($.handlebar())
.pipe($.wrap('handlebar.template()'))
.pipe($.declare)({
命名空间:“模板”,
noRedeclare:true,//避免重复声明
processName:函数(文件路径){
var matches=filePath.match(新的RegExp('(modules/(\\w+)/templates | templates)/((.*)\/?)(.*.js');
警报(火柴);
如果(!匹配){
返回文件路径;
}
返回(匹配[2]?匹配[2]+“/”:“”)+(匹配[3]?匹配[3]:“”);
}
}));
//将部分和模板输出为build/js/templates.js
返回合并(部分、模板)
.pipe($.concat('templates.js'))
.pipe($.wrap($define([“handlebar”],function(handlebar){返回此[“templates”];});'))
.pipe(gulp.dest(config.dest));
});
这是编译的templates.js
define(["handlebars"], function(Handlebars) {this["templates"] = this["templates"] || {};
this["templates"]["c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root-layout"] = this["templates"]["c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root-layout"] || {};
this["templates"]["c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root-layout"]["js"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
return "<header class=\"header\"></header>\n<div class=\"container\"></div>\n<footer class=\"footer\">\n <div class=\"row\">All rights reserved.\n </div>\n</footer>\n";
},"useData":true});
define([“handlebar]”),function(handlebar){this[“templates”]=this[“templates”]|{};
此[“模板”][“c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root布局”]=此[“模板”][“c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root布局”]|{;
这个[“templates”][“c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root layout”][“js”]=handlebar.template({“compiler”:[6,”>=2.0.0-beta.1“,“main”):函数(depth0,helpers,partials,data){
返回“\n\n\n保留所有权利。\n\n\n”;
},“useData”:true});
“c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root layout”在编译后的模板中几乎肯定是问题所在。当您在Mac上构建时,\\'s会发现编译后的templates.js文件必须只有这个[“templates”][“templates\\root layout”]而不是这个[“templates”][“c:\\Repos\\kamyoncular\\app\\scripts\\templates\\root布局”][“js”]。但我找不到确切的进程名配置。