jha*_*amm 14 javascript requirejs backbone.js handlebars.js
我正在建立一个Backbone项目,Handlebars我遇到了一个Handlebars没有找到编译方法的问题.这是我的配置文件:
require.config({
hbs: {
templateExtension: '.hbs'
},
paths: {
backbone: "libs/backbone/backbone",
handlebars: 'libs/handlebars/handlebars.amd',
hbs: 'libs/requirejs-hbs/hbs',
jquery: 'libs/jquery/jquery',
jqueryMockAjax: 'libs/jquery-mockjax/jquery.mockjax',
text: 'libs/requirejs-text/text',
templates: 'templates/',
underscore: 'libs/underscore/underscore'
},
shim: {
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
hbs: {
deps: ['handlebars'],
exports: 'hbs'
},
jqueryMockAjax: {
deps: [ 'jquery' ],
exports: '$.mockjax'
},
underscore: {
exports: '_'
}
}
});
require(['app'], function(App) {
'use strict';
var app = new App();
app.render();
});
Run Code Online (Sandbox Code Playgroud)
这是app.js我想要呈现的:
define(function(require) {
var Backbone = require('backbone');
var testTemplate = require('hbs!templates/test');
var router = Backbone.View.extend({
el: $('body'),
template: testTemplate,
render: function() {
return $(this.el).html(this.template());
}
});
return router;
});
Run Code Online (Sandbox Code Playgroud)
当Handlebars调用hbs.js在线25文件无法找到compile函数
define(["handlebars"], function(Handlebars) {
var buildMap = {},
templateExtension = ".hbs";
return {
// http://requirejs.org/docs/plugins.html#apiload
load: function (name, parentRequire, onload, config) {
// Get the template extension.
var ext = (config.hbs && config.hbs.templateExtension ? config.hbs.templateExtension : templateExtension);
if (config.isBuild) {
// Use node.js file system module to load the template.
// Sorry, no Rhino support.
var fs = nodeRequire("fs");
var fsPath = config.dirBaseUrl + "/" + name + ext;
buildMap[name] = fs.readFileSync(fsPath).toString();
onload();
} else {
// In browsers use the text-plugin to the load template. This way we
// don't have to deal with ajax stuff
parentRequire(["text!" + name + ext], function(raw) {
// Just return the compiled template
****HERE onload(Handlebars.compile(raw));
});
}
},
// http://requirejs.org/docs/plugins.html#apiwrite
write: function (pluginName, name, write) {
var compiled = Handlebars.precompile(buildMap[name]);
// Write out precompiled version of the template function as AMD
// definition.
write(
"define('hbs!" + name + "', ['handlebars'], function(Handlebars){ \n" +
"return Handlebars.template(" + compiled.toString() + ");\n" +
"});\n"
);
}
};
});
Run Code Online (Sandbox Code Playgroud)
该Handlebars变量为我提供了Handlebars环境,但它有一个额外的层,所以我必须将该行更改为Handlebars.default.compile(raw).该default物体来自何处以及如何摆脱它?我不担心,但如果我把这个项目拉到其他地方,我总是要记住这样做.
Igu*_*aut 62
我刚刚第一次使用Handlebars遇到了这个问题.您可能正在使用Handlebars的"运行时"构建.我把这个包含在我的要求中,错误地假设它是缩小版或其他东西.
但事实上,运行时版本因为排除了模板编译器而显着缩小,并且仅用于预编译模板.如果您正在编译模板客户端,那么您需要http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v3.0.3.js的完整版本 (注意:此链接可能已过期;你可能最好直接去handlebarsjs.com并寻找当前下载的"完整版",而不是运行时.)
否则,您可以按照Handlebars网站上的说明运行模板编译器.你需要节点.模板编译器生成一个JavaScript文件,其中包含您需要链接到页面的预编译模板代码以及Handlebars运行时构建.
jha*_*amm -2
这是我解决这个问题的方法,尽管我不完全理解为什么上面的配置不起作用。该hbs插件有一个文件夹,其中包含它所需的所有依赖项,例如handlebars. 当我引用目录handlebars中包含的副本hbs时,一切都按预期进行。我不明白为什么香草副本handlebars不起作用。我没有使用handlebars运行时,它是完整版本,但仍然存在问题。解决这个问题后,我的模板就可以工作了。
| 归档时间: |
|
| 查看次数: |
10618 次 |
| 最近记录: |