我试图弄清楚如何通过它的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
你在哪里看到示例将集合作为模型属性传递?我看到很多新人在做这件事。可能在互联网上有一些不好的来源 –