1

我尝试使用早午餐和车把设置开发环境。该handlebars-brunch包存储在我的node_moduleshandlebars.runtime.js包含在我的vendor.js文件中。我已经定义了这个模板:

你好.模板:

<p>Hello {{name}}</p>

并将这些行添加到我的config.coffee

    templates:
        defaultExtension: 'handlebars'
        joinTo: 'app.js'

作为它有效的证明,我可以在app.js以下几行中看到:

  ;var __templateData = Handlebars.template(function Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];

helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression;


buffer += "<p>Hello ";
if (stack1 = helpers.name) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
else { stack1 = depth0.name; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
buffer += escapeExpression(stack1)
  + "</p>\r\n\r\n";
return buffer;
});
if (typeof define === 'function' && define.amd) {
    define([], function() {
        return __templateData;
    });
} else if (typeof module === 'object' && module && module.exports) {
    module.exports = __templateData;
} else {
    __templateData;
}

但是当我这样调用时Handlebars.templates.hello

var tpl  = Handlebars.templates.hello;
var html = tpl ( { name: "ok" } );
console.log ( tpl );

我得到这个错误:Cannot read property 'hello' of undefined。所以 Handlebars 是定义的,但不是模板。我的依赖项包含似乎也很好,因为我的主要功能位于其他所有功能之后,如下所示:

[...]
if (typeof define === 'function' && define.amd) {
    define([], function() {
       return __templateData;
    });
} else if (typeof module === 'object' && module && module.exports) {
    module.exports = __templateData;
} else {
    __templateData;
}

;( function ( $, document, window ) { 
    var tpl  = Handlebars.templates.hello;
    var html = tpl ( { name: "ok" } );
    console.log ( tpl );
} ( jQuery, document, window ) );

有什么想法/建议吗?

4

1 回答 1

1

早午餐使用模块。它不分配全局变量或其他东西。

require('name')

如果你的文件是app/name.js

于 2013-10-25T11:41:35.890 回答