ルーター内のURLを動的に変更しようとしましたが、なんとかできませんでした。ベースのコレクションURLに戻り続けます。ここでは、3つの異なるURLを指すことを除けば、まったく同じことを行う3つの異なるコレクションを含むコードを投稿しました。そのモデルに依存するのは1つとmodel
3つだけで、同じビューをレンダリングすることさえあります。collections
動的に変更して、1つと1つurl
しか作成できないようにするにはどうすればよいですか?このような場合に最適ですか?Collection
Model
// MODELS & COLLECTIONS
window.Post = Backbone.Model.extend({
urlRoot: function() {
return 'http://localhost:5000/json/guides/:id'
}
})
App.Collections.RecentPosts = Backbone.Collection.extend({
model: Post,
url:'http://localhost:5000/json/posts/recent',
})
App.Collections.PopularPosts = Backbone.Collection.extend({
model: Post,
url:'http://localhost:5000/json/posts/popular',
})
App.Collections.FeaturedPosts = Backbone.Collection.extend({
model: Post,
url:'http://localhost:5000/json/posts/featured',
})
// CONTROLLER
App.Controllers.Documents = Backbone.Router.extend({
routes:{
"recent" : "recent",
"popular" : "popular",
"featured" : "featured",
},
recent: function(){
//.... same as featured ?
},
popular: function(){
//.... same as featured ?
},
featured: function(){
$("#browser").empty();
var collection = new App.Collections.Posts();
collection.fetch({
success: function(col,posts){
new App.Views.GuideView({collection: posts});
},
error: function(error){
console.log(error)
}
})
}
});