可以插入到另一个视图中的简单弹出视图。在创建时,它被引用到父视图的视图元素:
function(app) {
var Popover = app.module();
Popover.Views.Default = Backbone.View.extend({
className: 'popover',
initialize: function(options) {
this.reference = options.reference;
this.reference.bind('click', this.toggle());
this.render();
},
beforeRender: function() {
this.content = this.$el.find('.popover');
}
toggle: function() {
console.log('toggle');
}
});
// Required, return the module for AMD compliance.
return Popover;
});
引用是将设置弹出框的元素,如:
Main.Views.Start = Backbone.View.extend({
template: "main/start",
serialize: function() {
return { model: this.model };
},
initialize: function() {
this.popover = new Popover.Views.Default({
reference: this.$el.find('.member')
});
this.listenTo(this.model, "change", this.render);
},
beforeRender: function(){
this.insertView(this.popover);
}
});
弹出框显示正确,但是当我单击时a.member
没有任何反应。此外,我注意到在页面刷新时触发了切换方法,并且切换出现在日志中。很奇怪。任何想法为什么会发生这种情况?如何使用弹出框的切换方法连接父视图中的链接单击?
当我记录参考时:
[prevObject: jQuery.fn.jQuery.init[1], context: undefined, selector: ".member", jquery: "2.0.0", constructor: function…]context: undefinedlength: 0prevObject: jQuery.fn.jQuery.init[1]selector: ".member"__proto__: Object[0]