嘿,我尝试删除所有内容的尝试是在使用 this.close() 触发上一个或下一个函数后调用搜索视图文件中的关闭函数,但它正在删除完全影响导航功能的视图......我想知道如何去做完成后删除所有内容,否则在路由器中会多次出现vent.on。
这是我的路由器文件:
define([
'jquery',
'underscore',
'backbone',
'views/page',
'models/search',
'views/search',
'text!templates/search.html',
'models/song',
'text!templates/song.html'
], function($, _, Backbone, PageV, SearchM, SearchV, SearchT, SongM, SongT) {
var vent = _.extend({}, Backbone.Events);
var currentView, page, search;
Backbone.View.prototype.close = function () {
this.remove();
this.unbind();
if (this.onClose) {
this.onClose();
}
};
var AppRouter = Backbone.Router.extend ({
routes: {
'page/:id': 'showPage',
'search': 'showView'
}
});
var initialize = function () {
var app_router, songPages;
app_router = new AppRouter;
vent.on('loadPage', function (id) {
console.log('hit loaded page');
var newPage = 'page/' + id;
if(id < songPages && id >= 0 ) {
app_router.navigate(newPage, true);
} else {
app_router.navigate('search', true);
}
})
console.log('router file hit');
app_router.on('route:showPage', function (id) {
console.log('page rendered');
var songs, collected, songM, start;
songM = new SongM();
songM.localStorage = new Backbone.LocalStorage("music");
songM.localStorage.findAll().forEach(function (i) {
collected = i;
});
songPages = Math.ceil(collected.music.length / 25); //10 pages
start = id * 25;
songs = collected.music.splice(start, 25);
var titles = {
week: collected.week,
year: collected.year,
channel: collected. channel
};
var currentId = parseInt(id);
if (page) {console.log('page is current'); console.log(page); page.remove(); }
page = new PageV({model: songM, collection: songs, vent: vent, titles: titles, theId: currentId });
page.render(id);
//$("#Sirius").html(page.el);
});
app_router.on('route:showView', function () {
console.log('search page loading...');
var cur;
var searchM = new SearchM();
//if (search) {console.log(search); search.remove(); }
search = new SearchV({model: searchM, vent: vent});
//if(cur) {cur.stopListening(); cur.remove(); console.log('cur removed'); }
search.render();
//cur = search;
vent.on('nextPage', printCons);
function printCons () {
console.log('changing pages');
app_router.navigate('page/0', true);
};
});
Backbone.history.start();
};
return {
initialize: initialize
};
});
这是具有页面视图的页面:
define([
'jquery',
'underscore',
'backbone',
'models/song',
'collections/songs',
'views/song',
'text!templates/page.html',
'text!templates/song.html'
], function($, _, Backbone, Song, Songs, SongV, PageT, SongT){
var Page = Backbone.View.extend({
el: $("#Sirius"),
events: {
"click .prev": "previous",
"click .next": "next"
},
previous: function () {
this.options.vent.trigger('loadPage', this.options.theId - 1);
},
next: function () {
this.options.vent.trigger('loadPage', this.options.theId + 1);
},
render: function () {
var headings = this.options.titles;
var info = {
week: headings.week,
channel: headings.channel,
year: headings.year
}
var pagetemp = _.template( PageT, info);
this.$el.html( pagetemp );
var songColl = this.collection;
var songV = new SongV({collection: songColl});
songV.render();
}
});
return Page;
});