我在Backbone中遇到了一些問題。 這是我收集的物品的示例:在Backbone.js中爲集合中的一個元素創建詳細信息頁面
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 }));
},
});
現在我也想:
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循環 這是列出每個項目一個詳細信息視圖,我已經有了與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
}
});
我將非常感謝幫助。 謝謝。
感謝您的回答。 如何在代碼中添加所有這些? 對不起,如果這是太多要問,我只是一個真正的新手在骨幹和js:/如果你確實感到慷慨,我會永遠感激。 這是我現在的全部代碼:http://pastebin.com/hJwmijmZ – vlovystack
編輯我的代碼以顯示底部的安靜答案。在您的「詳細信息頁面的路由」中調用render()時,請不要忘記包含url_title! – user1026361
感謝您的更新,不幸的是它不起作用,雖然我不明白爲什麼..當你用另一個答案回覆時,被炒作一秒鐘,但我想我做錯了什麼。 – vlovystack