Javascript 再次讨论主干僵尸视图
我正在努力理解主干,目前正在与僵尸观点作斗争。我已经读了很多关于这个问题的文章,但是我仍然无法理解 为了简单起见,我设置了两个需要切换的视图(没有数据)。 到目前为止,我所做的是:Javascript 再次讨论主干僵尸视图,javascript,backbone.js,Javascript,Backbone.js,我正在努力理解主干,目前正在与僵尸观点作斗争。我已经读了很多关于这个问题的文章,但是我仍然无法理解 为了简单起见,我设置了两个需要切换的视图(没有数据)。 到目前为止,我所做的是: 创建对象 //定义应用程序对象 变量应用={ 通风口:{}, 模板:{}, 视图:{}, 路由器:{}, }; //实例化事件聚合器并将其附加到应用程序 app.vent=\ u0.extend({},Backbone.Events); 定义两个非常简单的模板(存储在app.templates中):第一个模板有一些伪
你能帮我吗?非常感谢。长帖,如果您有任何问题,请询问 僵尸视图只是一个不在DOM中的视图,它侦听事件并对事件做出反应——有时这种行为是预期的,但通常不是 如果未正确删除视图的DOM事件处理程序,则不会对视图及其内存中的HTML片段进行垃圾收集。如果主干.Event处理程序未正确解除绑定,则可以 //define application object var app = { vent: {}, templates: {}, views: {}, routers: {}, }; //instantiate event aggregator and attach it to app app.vent = _.extend({}, Backbone.Events); app.views.instructions = Backbone.View.extend({ //load underscore template template: _.template(app.templates.instructions), //automatically called upon instantiation initialize: function(options) { //bind relevant fucntions to the view _.bindAll(this, 'render', 'testBegin', 'stillAlive', 'beforeClose'); //listen to app.vent event this.listenTo(app.vent, 'still:alive', this.stillAlive); }, //bind events to DOM elements events: { 'click #test-begin' : 'testBegin', }, //render view render: function() { this.$el.html(this.template()); return this; }, //begin test testBegin: function() { Backbone.history.navigate('begin', {trigger: true}); }, //still alive stillAlive: function() { console.log('I am still alive'); }, //before closing beforeClose: function() { //stop listening to app.vent this.stopListening(app.vent); }, }); //test view app.views.test = Backbone.View.extend({ //load underscore template template: _.template(app.templates.test), //automatically called upon instantiation initialize: function(options) { //trigger still:alive and see if removed view responds to it app.vent.trigger('still:alive'); //bind relevant fucntions to the view _.bindAll(this, 'render'); }, //render view render: function() { this.$el.html(this.template()); return this; }, }); //base router app.routers.baseRouter = Backbone.Router.extend({ //routes routes: { '': "instructions", 'begin': "beginTest" }, //functions (belong to object controller) instructions: function() {baseController.instructions()}, beginTest : function() {baseController.beginTest()}, }); //baseRouter controller var baseController = { instructions: function() { mainApp.viewsManager.rederView(new app.views.instructions()); }, beginTest: function(options) { mainApp.viewsManager.rederView(new app.views.test()); }, }; //define mainApplication object mainApp = {}; //manages views switching mainApp.viewsManager = { //rootEl rootEl: '#test-container', //close current view and show next one rederView : function(view, rootEl) { //if DOM el isn't passed, set it to the default RootEl rootEl = rootEl || this.rootEl; //close current view if (this.currentView) this.currentView.close(); //store reference to next view this.currentView = view; //render next view $(rootEl).html(this.currentView.render().el); }, }; //render first view of app mainApp.viewsManager.rederView(new app.views.instructions()); //initiate router and attach it to app mainApp.baseRouter = new app.routers.baseRouter(); //start Backbone history Backbone.history.start({silent: true }); //add function to Backbone view prototype (available in all views) Backbone.View.prototype.close = function () { //call view beforeClose function if it is defined in the view if (this.beforeClose) this.beforeClose(); //this.el is removed from the DOM & DOM element's events are cleaned up this.remove(); //unbind any model and collection events that the view is bound to this.stopListening(); //check whether view has subviews if (this.hasOwnProperty('_subViews')) { //loop thorugh current view's subviews _(this._subViews).each(function(child){ //invoke subview's close method child.close(); }); } };
mainApp.viewsManager.rederView(new app.views.test());
//baseRouter controller
var baseController = {
instructions: function() {
mainApp.viewsManager.rederView(app.views.instructions);
},
beginTest: function(options) {
mainApp.viewsManager.rederView(app.views.test);
},
};
rederView : function(ViewClass, rootEl) {
//if DOM el isn't passed, set it to the default RootEl
rootEl = rootEl || this.rootEl;
//close current view
if (this.currentView) this.currentView.close();
//store reference to next view
this.currentView = new ViewClass();
//render next view
$(rootEl).html(this.currentView.render().el);
},
//unbind any model and collection events that the view is bound to
this.stopListening();