Hi, my attempt to delete everything caused the close function in the search view file after running the previous or next function using this.close (), but it deleted the view that completely affected the navigation function ... I was wondering how to delete everything after completion otherwise vent.on happens several times in the router.
Here is my router file:
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);
Here is the page with the page:
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; });