简体   繁体   English

重新初始化主干视图后未触发事件

[英]Events not firing after reinitialising backbone views

I'm trying to tidy code in my Backbone project.我正在尝试整理 Backbone 项目中的代码。 One of the problems I face is that I initialise all my views in the render's initialise function.我面临的问题之一是我在渲染的初始化函数中初始化所有视图。 I've now decided to only initialise a single view (and it's children) at a time.我现在决定一次只初始化一个视图(以及它的子视图)。

The rendering of the pages works and I can swap backward and forward between views.页面的渲染有效,我可以在视图之间前后交换。 Events that are bound in the view fire after a hard refresh of the page (F5 etc) but once I've moved to another view, the events no longer fire.硬刷新页面(F5 等)后,绑定在视图中的事件会触发,但是一旦我移动到另一个视图,这些事件就不再触发。 I don't understand why as the previous view should be totally removed on second initialisation.我不明白为什么应该在第二次初始化时完全删除先前的视图。 We should then get a clean render, just as it would be on first load after a refresh.然后我们应该得到一个干净的渲染,就像刷新后第一次加载一样。 Can anyone explain why the events aren't firing?谁能解释为什么事件没有触发?

Below are code examples to demonstrate the problem:下面是演示问题的代码示例:

newView: function(view){
            //Check if the holding variable is defined. If it is then  
            //call the close function
            if (routerView == undefined){
                console.log("routerview is undefined")
            } else {
                // This calls a function on the view which will remove any
                //children, once it's done that it will remove its self.
                routerView.close();
            }

            // When removing the view it removes the parent element in the index file.
            // Here we add the container back in and set it to the new view's el
            if ( $('#AppContainer').length == 0 ){
                // Add new div container to the app and assign it to the 'el'
                view.el = $('body').prepend('<div id="AppContainer"></div>');                   
            }
            // Return view to the route for rendering.      
            return routerView;
        },

The close function inside one of the views would look something like this:其中一个视图中的 close 函数如下所示:

close: function(){
    //Deal with any child views here as well.
    this.remove();
},

Finally, in the route where we'd call the newView function would look最后,在我们调用 newView 函数的路径中会看起来

admin: function(){
    // Call the newView function with a new instance of the AdminView and then assign it back        
    this.adminView = router.newView( new AdminView({ el : $("#AppContainer")} ));
    //Render the view
    this.adminView.render();

}, 

I have done some more work investigating the problem and I've discovered it.我已经做了一些更多的工作来调查这个问题,我已经发现了它。 The problem was two fold but appeared on the same line of code.问题是双重的,但出现在同一行代码中。

view.el = $('body').prepend('<div id="AppContainer"></div>');   

I discovered on the backbone docs that you should use the setElement function to alter a view's element.我在主干文档中发现您应该使用 setElement 函数来更改视图的元素。 This then transfers all bound events which now means they work.然后传输所有绑定事件,这意味着它们现在可以工作。

I then discovered that $('body').prepend('<div id="AppContainer"></div>') would return a reference to body and not the new #AppContainer but it actually returns a reference to the body which meant that the content of view was being placed in the body.然后我发现$('body').prepend('<div id="AppContainer"></div>')会返回对 body 的引用而不是新的#AppContainer但它实际上返回对 body 的引用意味着视图的内容被放置在正文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM