Backbone.js和车把-使用grunt contrib车把
我只是想知道是否有人有在主干网项目中使用此插件的经验 我不想将所有脚本模板标记放在一个索引文件中,而是希望将模板放在需要它们的视图所在的同一目录中 因此,我希望我可以使用node选项来要求本地模板并对其进行渲染,然后将其附加到索引文件上的#id(我将在最后整理) 所以基本上我有我的Handlebar模板(template.hbs)和它编译的js(template.js)以及我的主干视图index.coffeeBackbone.js和车把-使用grunt contrib车把,backbone.js,handlebars.js,gruntjs,Backbone.js,Handlebars.js,Gruntjs,我只是想知道是否有人有在主干网项目中使用此插件的经验 我不想将所有脚本模板标记放在一个索引文件中,而是希望将模板放在需要它们的视图所在的同一目录中 因此,我希望我可以使用node选项来要求本地模板并对其进行渲染,然后将其附加到索引文件上的#id(我将在最后整理) 所以基本上我有我的Handlebar模板(template.hbs)和它编译的js(template.js)以及我的主干视图index.coffee public |_ coffee |_views |
public
|_ coffee
|_views
|_card
|_list
index.coffee
template.hbs
template.js
作为参考,我的grunt文件如下所示:
handlebars: {
compile: {
options: {
namespace: 'MyApp.Templates',
node: true
},
files: {
"public/coffee/views/card/list/template.js": "public/coffee/views/card/list/template.hbs"
}
}
},
在我的主干视图(index.coffee)中,我希望需要把手模板,如下所示:
class CardList extends Backbone.View
template: require('./template')
…
do some other shiz
…
render: =>
template = Handlebars.compile($(this.template).html())
html = template
model: this.model
$(this.el).html(html)
渲染这是在检查器中显示此错误:
Uncaught [object Object]
> template = handlebars.compile($(this.template).html());
我显然不知道我在做什么,所以我希望有人能告诉我如何正确使用这个插件
我正在使用grunt contrib手柄v0.3.5
感谢您的帮助
谢谢查看包含的template.js文件。grunt comtrib Handlbar应该已经为您将其预编译为Javascript函数,因此不再需要调用handlebar.compile。您可以删除模板=handlebar.compile行。您可以通过以下方式实现这一点 也许是这样,尽管我不确定
cwd
是否支持全局模式。我也不确定dest
是否与cwd
相关。如果不是这样的话,这是行不通的,但值得一试
handlebars: {
dist: {
options: {
namespace: 'MyApp.Templates',
node: true
},
// Glob for a directory of files, builds the files object, then map each
// one to a new destination file.
expand: true,
cwd: './public/coffee/views/*',
src: '**/*.hbs',
dest: './',
ext: '.js'
}
},