gpt4 book ai didi

javascript - Backbone View 事件未触发

转载 作者:行者123 更新时间:2023-11-29 18:31:00 25 4
gpt4 key购买 nike

我有一个正在扩展的模态对话框类,以便显示带有一些按钮的表单。这是 ModalView View :

App.ModalView = Backbone.View.extend({
events: {
"click .dismiss": "dismiss"
},
element: "<section class='modal'><div class='overlay'></div><div class='content'></div></section>",

initialize: function () {
this.el = $(this.element);

this.setupElement();
this.bindContext();
this.extendEvents();
this.render();
this.miscellaneous();
},

bindContext: function () {
_.bindAll(this, "dismiss", "render", "setBoxStyle");
},

setupElement: function () {
this.template = $("#modal-template");
},

miscellaneous: function () {},
extendEvents: function () {},

render: function () {
$(this.el).find(".content").html(this.template.html());
$("body").append(this.el);

this.setBoxStyle();

if (this.options.content) {
$(this.el).find(".content").html(this.options.content);
}
},

getMargin: function (width, height) {
var top, left;

width = parseFloat(width);
height = parseFloat(height);

top = Math.max(0, Math.round((window.innerHeight - height) / 2));
left = Math.max(0, Math.round((window.innerWidth - width) / 2));

return top + "px " + left + "px";
},

setBoxStyle: function () {
var css = this.options.css || {};

var el = $(this.el).find(".content");

el.css(css);

var width = el.outerWidth();
var height = el.outerHeight();
css.margin = this.getMargin(width, height);

el.css(css);
},

dismiss: function () {
this.remove();
}
});

扩展 View :

App.AddRecordView = App.ModalView.extend({
setupElement: function () {
this.template = $("#add-record-template");
}
});

这是模板:

<script type="text/template" id="add-record-template">
<h1>Add Record</h1>

<button class="green save">Save Record</button>
<button class="cancel dismiss">Cancel</button>

</script>

下面是我如何初始化 View :

this.addRecordView = new App.views.addRecord({
model: this.model,
css: {
width: "400px"
}
});

出于某种原因,当我单击按钮时,关闭事件永远不会触发。这是怎么回事?

最佳答案

您还没有为 View 定义 el 或 tagName。如果以上均未声明,则 Backbone 默认将委托(delegate)事件分配给 div。然后在初始化函数中显式设置 el,用附加的事件处理程序覆盖 el。尝试将您的部分设置为 tagName 并将其从您的元素属性中删除。然后将您的元素附加到 this.el。很抱歉没有提供代码示例,但我是在手机上写的。

关于javascript - Backbone View 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8390382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com