简体   繁体   English

Backbone刷新视图事件

[英]Backbone refresh view events

In my view I don't declare this.el because I create it dinamically, but in this way the events don't fire. 在我看来,我没有声明this.el,因为我以恐怖的方式创建它,但是这样事件就不会发生。

This is the code: 这是代码:

View 1: 查看1:

App.Views_1 = Backbone.View.extend({

    el:             '#content',

    initialize:     function() {    
                        _.bindAll(this, 'render', 'renderSingle');                          
                    },

    render:         function() {    
                        this.model.each(this.renderSingle);                 
                    },

    renderSingle:   function(model) {

                        this.tmpView = new App.Views_2({model: model});                     
                        $(this.el).append( this.tmpView.render().el );

                    }
});

View 2: 观点2:

App.Views_2 = Backbone.View.extend({

    initialize:     function() {                                
                        _.bindAll(this, 'render');                      
                    },

    render:         function() {    
                        this.el = $('#template').tmpl(this.model.attributes);       // jQuery template                          
                        return this;                            
                    },

    events:         {       
                        'click .button' :       'test'                  
                    },

    test:           function() {        
                        alert('Fire');  
                    }

    });

});

When I click on ".button" nothing happens. 当我点击“.button”时没有任何反应。 Thanks; 谢谢;

At the end of your render() method, you can tell backbone to rebind events using delegateEvents() . 在render()方法结束时,您可以告诉骨干使用delegateEvents()重新绑定事件。 If you don't pass in any arguments, it will use your events hash. 如果您没有传入任何参数,它将使用您的事件哈希。

render:         function() {    
                    this.el = $('#template').tmpl(this.model.attributes);       // jQuery template                          
                    this.delegateEvents();
                    return this;                            
                }

As of Backbone.js v0.9.0 (Jan. 30, 2012), there is the setElement method to switching a views element and manages the event delegation. 从Backbone.js v0。9。0(2012年1月30日)开始,有一个setElement方法来切换视图元素并管理事件委托。

render: function() {
    this.setElement($('#template').tmpl(this.model.attributes));
    return this;
}

Backbone.View setElement : http://backbonejs.org/#View-setElement Backbone.View setElementhttp//backbonejs.org/#View-setElement

setElementview.setElement(element) setElementview.setElement(元件)

If you'd like to apply a Backbone view to a different DOM element, use setElement, which will also create the cached $el reference and move the view's delegated events from the old element to the new one. 如果您想将Backbone视图应用于其他DOM元素,请使用setElement,它还将创建缓存的$ el引用,并将视图的委托事件从旧元素移动到新元素。



Dynamically creating your views in this fashion has it's pros and cons, though: 以这种方式动态创建视图有其优点和缺点:

Pros: 优点:

  • All of your application's HTML markup would be generated in templates, because the Views root elements are all replaced by the markup returned from the rendering. 所有应用程序的HTML标记都将在模板中生成,因为Views根元素全部由渲染返回的标记替换。 This is actually kind of nice... no more looking for HTML inside of JS. 这实际上很好......不再需要在JS中查找HTML。
  • Nice separation of concerns. 很好的分离关注点。 Templates generate 100% of HTML markup. 模板生成100%的HTML标记。 Views only display states of that markup and respond to various events. 视图仅显示该标记的状态并响应各种事件。
  • Having render be responsible for the creation of the entire view (including it's root element) is in line with the way that ReactJS renders components, so this could be a beneficial step in the process of migrating from Backbone.Views to ReactJS components. 渲染负责创建整个视图(包括它的根元素)与ReactJS渲染组件的方式一致,因此这可能是从Backbone.Views迁移到ReactJS组件的过程中的一个有益步骤。

Cons: - these are mostly negligible 缺点: - 这些几乎可以忽略不计

  • This wouldn't be a painless transition to make on an existing code base. 这不是在现有代码库上进行的无痛过渡。 Views would need to be updated and all templates would need to have the View's root elements included in the markup. 需要更新视图,并且所有模板都需要在标记中包含View的根元素。
    • Templates used by multiple views could get a little hairy - Would the root element be identical in all use cases? 多个视图使用的模板可能会有点毛茸茸 - 根元素在所有用例中都是相同的吗?
  • Prior to render being called, the view's root element is useless. 在调用render之前,视图的根元素是无用的。 Any modifications to it will be thrown away. 对它的任何修改都将被丢弃。
    • This would include parent views setting classes/data on child view elements prior to rendering. 这将包括在渲染之前在子视图元素上设置类/数据的父视图。 It is also bad practice to do this , but it happens -- those modifications will be lost once render overrides the element. 这样做也是不好的做法 ,但它会发生 - 一旦渲染覆盖元素,这些修改就会丢失。
  • Unless you override the Backbone.View constructor, every view will unnecessarily delegate it's events and set attributes to a root element that is replaced during rendering. 除非重写Backbone.View构造函数,否则每个视图都会不必要地委托它的事件并将属性设置为在渲染期间替换的根元素。
  • If one of your templates resolves to a list of elements rather than a single parent element containing children, you're going have a bad time . 如果您的某个模板解析为元素列表而不是包含子元素的单个父元素, 那么您将遇到错误的时间 setElement will grab the first element from that list and throw away the rest. setElement将从该列表中获取第一个元素并丢弃其余元素。
    • Here's an example of that problem, though: http://jsfiddle.net/CoryDanielson/Lj3r85ew/ 以下是该问题的一个例子: http//jsfiddle.net/CoryDanielson/Lj3r85ew/
    • This problem could be mitigated via a build task that parses the templates and ensures they resolve to a single element, or by overriding setElement and ensuring that the incoming element.length === 1 . 可以通过解析模板并确保它们解析为单个元素的构建任务,或通过重写setElement并确保传入的element.length === 1来缓解此问题。

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

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