我所拥有的是以下内容:
Views.Parent = Backbone.View.extend( {
template: "parent",
initialize: function( ) {
this.render( );
},
beforeRender: function( ) {
this.setView( ".foo", new Views.Child( {
model: this.model
} );
},
afterRender: function( ) {
this.$el.html( Handlebars.compile( this.$el.html( ) ).call( this, this.model.attributes ) );
var foo = this.getView( ".foo" );
foo.delegateEvents( );
}
} );
Views.Child = Backbone.View.extend( {
template: "child",
events: {
"click input": "inputClick"
},
initialize: function( ) {
this.listenTo( this.model, "change", this.render );
},
inputClick: function( ) {
console.info( "Input Clicked" );
},
afterRender: function( ) {
this.$el.html( Handlebars.compile( this.$el.html( ) ).call( this, this.model.attributes ) );
var foo = this.getView( ".foo" );
foo.delegateEvents( );
}
} );
Views.Child
中的事件未触发。肯定会找到视图,foo.events将返回正确的事件。我已经尝试了多种插入子视图的方法,delegateEvents就是不这样做。
还有其他人参与此活动并提供任何见解吗?
我应该提到我在子视图中使用了events属性和this.listenTo。他们都没有被解雇。
编辑:父母正在被添加到另一个视图中:
this.$el.append( parentView.$el );
其中parentView
是Views.Parent
的实例化对象。