我有一個BackboneJS應用程序,它有兩個不同視圖的路由器。問題是,如果我從一個視圖轉到另一個視圖,它可以正常工作,但是如果我單擊瀏覽器上的後退按鈕,它只會將其中一個視圖添加到另一個視圖中。 (所以我最終看到兩個視圖)。BackboneJS移除舊視圖?
如何刪除視圖或強制刷新?
var Schedule = Parse.Object.extend({
className: "schedule"
});
var Movie = Parse.Object.extend({
className: "movie"
});
var ScheduleList = Parse.Collection.extend({
model: Schedule
});
var schedule = new ScheduleList();
var movie = new Movie();
var ScheduleView = Parse.View.extend({
initialize: function() {
schedule.query = new Parse.Query(Schedule);
schedule.query.ascending("date");
schedule.query.limit('500');
var render = this.render;
schedule.fetch({
success: function() {
render(schedule.toJSON());
}
});
},
render: function(schedule) {
console.log(schedule);
var template = Handlebars.compile($("#schedule-item").html());
$("#schedule-holder").html(template({shows: schedule}));
return this;
}
});
var MovieView = Parse.View.extend({
initialize: function() {
var query = new Parse.Query(Movie);
query.equalTo("mId", parseInt(this.id));
query.limit('1');
var render = this.render;
query.first({
success: function(details) {
render(details.toJSON());
}
});
},
render: function(movie) {
var template = Handlebars.compile($("#movie-item").html());
$("#movie-holder").html(template(movie));
return this;
}
});
var AppRouter = Parse.Router.extend({
routes: {
"movie/:id": "movieDetails",
"*actions": "schedule" // Backbone will try match the route above first
},
movieDetails: function(id) {
// Note the variable in the route definition being passed in here
var App = new MovieView({ id: id });
},
schedule: function(actions){
var App = new ScheduleView();
}
});
// Instantiate the router
var app_router = new AppRouter;
// Start Backbone history a neccesary step for bookmarkable URL's
Parse.history.start();
類似問題http://stackoverflow.com/questions/9079491/cleaning-views-with-backbone- js – James