Mustache.js / Hogan.js 与 Backbone.js

发布于 2024-12-25 21:26:58 字数 1412 浏览 0 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

得不到的就毁灭 2025-01-01 21:26:58

看起来您正在将一个 Collection 而不是单个模型传递给 Hogan。

试试这个:

_.each(myAlbum,function(album) {
    this.el.append(template.render(album.toJSON()));
});

Looks like you're passing a Collection to Hogan rather than an individual model.

Try this instead:

_.each(myAlbum,function(album) {
    this.el.append(template.render(album.toJSON()));
});
二货你真萌 2025-01-01 21:26:58

我在 Backbone 集合上使用 Hogan 模板时遇到了同样的困境。

var o = {},
c = Album.toJSON();
o.data = c;
this.$el.html(template.render(o));

在我的模板中,我引用 {{#data}} {{/data}} 来迭代集合。

I ran into the same predicament, using Hogan templating on a Backbone collection.

var o = {},
c = Album.toJSON();
o.data = c;
this.$el.html(template.render(o));

and in my template i am referencing {{#data}} {{/data}} to iterate through the collection.

拧巴小姐 2025-01-01 21:26:58

我覆盖了骨干渲染调用,

Backbone.Marionette.Renderer.render = (template, data) ->
  HoganTemplates[template].render data

与木偶配合得很好。不过,backbone.forms 有问题。

I overrode the backbone render call

Backbone.Marionette.Renderer.render = (template, data) ->
  HoganTemplates[template].render data

Works very well with marionette. having issues with backbone.forms though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文