Mustache.js / Hogan.js 与 Backbone.js
我正在尝试使用backbone.js 实现一些代码
和 hogan.js (http://twitter.github.com/hogan.js/)
Hogan.js 是针对 Mustache 测试套件开发的,
因此,对于模板来说,所有适用的内容都是
此处指定的 hogan.js 也是如此。
我的问题是将 Backbone.Collection 传递给 Hogan/Mustache。
对于像这样的简单模板:
{{name}}
Hogan/Mustache 期望这样的东西可以正常工作:
{"name":"How Bizarre","artist":"OMC"}
但是我的 Backbone.Collection 是:
a)
[{"name":"How Bizarre","artist":"OMC"}]
或者这个:
b) [{"name":"How Bizarre","artist":"OMC"}, {"name":"性治疗","artist":"Marvin Gaye"}]
来自演示页面 http://mustache.github.com/#demo 我不能
迭代 a) 或 b) Backbone.Collection 对象。
谁能指出我该怎么做?
var Song = Backbone.Model.extend({
defaults: {
name: "Not specified",
artist: "Not specified"
}
});
var Album = Backbone.Collection.extend({
model: Song
});
var song1 = new Song({ name: "How Bizarre", artist: "OMC" });
var song2 = new Song({ name: "Sexual Healing", artist: "Marvin Gaye" });
var myAlbum = new Album;
myAlbum.add(song1);
myAlbum.add(song2);
我正在尝试通过传递我的 Backbone.Colleciton 进行渲染 像这样的对象: myAlbum.toJSON()
var template = "{{name}}!";
var template = Hogan.compile(template);
this.el.html(template.render(myAlbum.toJSON()));
谢谢。
I'm trying to implement some code using backbone.js
and hogan.js (http://twitter.github.com/hogan.js/)
Hogan.js was developed against the mustache test suite,
so everything that holds true for templates as
specified here, is also the case for hogan.js.
My problem is passing the Backbone.Collection to Hogan/Mustache.
For a simple template like this:
{{name}}
Hogan/Mustache expecting something like this works fine:
{"name":"How Bizarre","artist":"OMC"}
However my Backbone.Collection is:
a)
[{"name":"How Bizarre","artist":"OMC"}]
Or this:
b)
[{"name":"How Bizarre","artist":"OMC"}, {"name":"Sexual Healing","artist":"Marvin Gaye"}]
From the demo page http://mustache.github.com/#demo I cannot
iterate over either of the a) or b) Backbone.Collection obejcts.
Can anyone point out how I can do this?
var Song = Backbone.Model.extend({
defaults: {
name: "Not specified",
artist: "Not specified"
}
});
var Album = Backbone.Collection.extend({
model: Song
});
var song1 = new Song({ name: "How Bizarre", artist: "OMC" });
var song2 = new Song({ name: "Sexual Healing", artist: "Marvin Gaye" });
var myAlbum = new Album;
myAlbum.add(song1);
myAlbum.add(song2);
I am passing trying to render by passing my Backbone.Colleciton
object like this: myAlbum.toJSON()
var template = "{{name}}!";
var template = Hogan.compile(template);
this.el.html(template.render(myAlbum.toJSON()));
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在将一个 Collection 而不是单个模型传递给 Hogan。
试试这个:
Looks like you're passing a Collection to Hogan rather than an individual model.
Try this instead:
我在 Backbone 集合上使用 Hogan 模板时遇到了同样的困境。
在我的模板中,我引用 {{#data}} {{/data}} 来迭代集合。
I ran into the same predicament, using Hogan templating on a Backbone collection.
and in my template i am referencing {{#data}} {{/data}} to iterate through the collection.
我覆盖了骨干渲染调用,
与木偶配合得很好。不过,backbone.forms 有问题。
I overrode the backbone render call
Works very well with marionette. having issues with backbone.forms though.