Backbone刷新视图事件

kee*_*web 10 javascript jquery backbone.js

在我看来,我没有声明this.el,因为我以恐怖的方式创建它,但是这样事件就不会发生.

这是代码:

查看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 );

                    }
});
Run Code Online (Sandbox Code Playgroud)

观点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');  
                    }

    });

});
Run Code Online (Sandbox Code Playgroud)

当我点击".button"时没有任何反应.谢谢;

Edw*_*ith 20

在render()方法结束时,您可以告诉骨干使用delegateEvents()重新绑定事件.如果您没有传入任何参数,它将使用您的事件哈希.

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