我試圖弄清楚如何通過它的id來獲取模型並顯示相同的視圖。我已經設置我的路由器以通過ID獲取數據。我嘗試了很多,但無法找到解決方案。任何想法在此將不勝感激。從骨幹js集合中獲取ID的模型
collection.js文件:
app.Collections.qualityCollection = Backbone.Collection.extend({
model: app.Models.qualityModel,
url: "http://localhost/socialapp/php/fetch.php"
});
var userCollections = new app.Collections.qualityCollection();
userCollections.fetch();
router.js文件:
app.Routers.socialRouter = Backbone.Router.extend({
routes: {
"" : "homePage",
"make_friends/:id" : "userProfile",
},
userProfile: function() {
new app.Views.idView({ model: userCollections.get(id) });
}
});
new app.Routers.socialRouter();
Backbone.history.start();
view.js文件:
app.Views.idView = Backbone.View.extend({
el: $("#app"),
template: _.template($('#public-profile').html()),
initialize: function(){
this.render();
},
render: function() {
console.log(this.model); /// undefined
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
JSON從URL數據: [ { "id": "1", "firstname": "Abc", "lastname": "Xyz" }, { "id": "2", "firstname": "Klm", "lastname": "Opq" } ]
預計路由器鏈接:http://localhost/socialapp/#/make_friends/2
你在哪裏看到示例將集合作爲模型屬性傳遞?我看到很多新人在做這件事。可能在互聯網上有一些不好的來源 –