我試圖動態更改路由器內部鏈接,但無法管理做到這一點,它一直返回到基地集合URL。在這裏,我發佈了3個不同的集合的代碼,除了指向三個不同的網站,他們完全一樣。 我只有一個model
和三個collections
依賴於這種模式,他們甚至呈現了同樣的觀點。我如何可以動態改變url
,所以我只能創建一個Collection
和一個Model
?這是最好的例子嗎?動態更改URL中的骨幹
// 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)
}
})
}
});
謝謝!它效果很好。這是我最初的方法,但是我沒有初始化'Router'並且做錯了:'fetch({url:config.url},success:....'。我也在'Router'中'Home' *函數* this.navigate(「popular」)'s。這不起作用...爲什麼會這樣? – Maroshii
我想因爲'navigate'被默認默認調用,試着做'this.navigate (「流行」,{trigger:true});' – Stephen
再次感謝....一天的英雄! – Maroshii