sách gpt4 ai đã đi

backbone.js - Mustache.js/Hogan.js và Backbone.js

In lại 作者:行者123 更新时间:2023-12-01 04:06:49 30 4
mua khóa gpt4 Nike

我正在尝试使用backbone.js 实现一些代码
和 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.



我的问题是将 Backbone.Collection 传递给 Hogan/Mustache。

对于像这样的简单模板:
{{name}}

Hogan/Mustache 期待这样的效果很好:
{"name":"How Bizarre","artist":"OMC"}

但是我的 Backbone.Collection 是:

一种)
[{"name":"How Bizarre","artist":"OMC"}]

或这个:

b)
[{"name":"How Bizarre","artist":"OMC"}, {"name":"Sexual Healing","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()));

谢谢你。

1 Câu trả lời

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

Hãy thử cách này:

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

关于backbone.js - Mustache.js/Hogan.js 和 Backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8792490/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com