我在 Backbone 中遇到了一些问题。这是我收藏中的一个项目的示例:
var gifs = new GifCollection([
{image: "images/gifs/wolf.gif",
url_title: "Wolf",
ratings: [0,0],
rating: 3, comments: "Awesome picture.",
title:"Wolf", category:"Animals"},
]);
现在在我的普通/主页视图中,我通过 FOR 循环列出每个项目这是:
var Fill_me_with_gifs = Backbone.View.extend({
el: "#fill_me_with_gifs",
render: function () {
var template = $("#user-view-template").html();
var gifs_html = '';
var gifs = this.collection.models;
for (var gif in gifs)
{
gifs_html += '<tr>';
gifs_html += '<td>' + "<h4>" + gifs[gif].get("title") + "</h4>" + '</td>';
gifs_html += '</tr>';
gifs_html += '<tr>';
gifs_html += '<td>' + "<img src=\"" + gifs[gif].get("image") + "\"" + " width=\"500\"" + ">"+ '</td>';
gifs_html += '</tr>';
gifs_html += '<tr>';
gifs_html += '<td>' + "<a href=\"#detail/" + gifs[gif].get("url_title") + "\"> View comments, rate and more</a>" + "</td>";
gifs_html += '</tr>';
}
this.$el.html( _.template( template, { gifs: gifs_html }));
},
});
现在我还想要一个详细信息视图,我已经拥有带有 url_title 的唯一链接(见上文)的每张图片 例如,当我单击第一个 gif 下方的链接时,我现在被重定向到 #detail/Wolf 而不是去通过 for 循环,我只需要一个 gif 在我看来。
我显然需要用 url_title 来做这件事,但我不知道到底是怎么回事..
所以现在我只有这个作为详细视图:
var Fill_me_with_one_gif = Backbone.View.extend({
el: "#fill_me_with_gifs",
render: function (url_title) {
var template = $("#user-view-template").html();
var gifs_html = '';
var gifs = this.collection.models;
//NO FOR LOOP BUT ONLY SINGLE ELEMENT FROM COLLECTION
//I don't know what to put here :|
},
});
这是我的详细信息页面路线
router.on("route:detail", function (url_title) {
$(".nav li").removeClass("active");
$("#nav-detail").addClass("active");
console.log("Show Detail Page for " + url_title);
var fill_me_with_one_gif = new Fill_me_with_one_gif({ collection: gifs });
fill_me_with_one_gif.render();
这是我的路由器:
var Router = Backbone.Router.extend({
routes: {
"" : "home",
"categories" : "categories",
"popular" : "popular",
"detail" : "detail",
"detail/:url_title" : "detail" //this is the one I'm having trouble with
}
});
我将非常感谢这方面的帮助。谢谢你。