主干视图事件未触发
我有一个模式对话框类,我正在扩展它以显示带有一些按钮的表单。这是 ModalView 视图:
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();
}
});
这是扩展视图:
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>
这是我初始化视图的方式:
this.addRecordView = new App.views.addRecord({
model: this.model,
css: {
width: "400px"
}
});
由于某种原因,当我单击按钮时,解雇事件永远不会触发。这是怎么回事?
I have a modal dialog class that I am extending in order to show a form with some buttons. Here is the 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();
}
});
Here is the extended view:
App.AddRecordView = App.ModalView.extend({
setupElement: function () {
this.template = $("#add-record-template");
}
});
Here is the 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>
Here is how I initialize the view:
this.addRecordView = new App.views.addRecord({
model: this.model,
css: {
width: "400px"
}
});
For some reason the dismiss event never fires when I click the button. What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未为您的视图定义 el 或 tagName。如果以上均未声明,则默认情况下 Backbone 会将委托事件分配给 div。然后在初始化函数中显式设置 el,并使用附加的事件处理程序覆盖 el。尝试将您的部分设置为 tagName 并将其从元素属性中删除。然后将您的元素附加到 this.el。
抱歉没有提供代码示例,但我正在手机上编写。
You have not defined an el or a tagName for your view. Backbone assigns the delegate events to a div by default if none of the above are declared. In your initialize function you then set the el explicitly, overriding the el with the eventhandlers attached. Try to set your section as tagName and remove it from your element property. Then append your elements to this.el.
Sorry for not providing a code example but I am writing on my phone.
没有正确的代码我只能猜测,
但这种错误通常是因为你的视图代码是在 dom 准备好之前声明的。
您能告诉我们您从哪里开始申请吗?
它在 jquery 文档就绪函数中吗?
without the proper code i can only guess,
but this kind of error usually comes because your view's code is declared before the dom is ready.
could you show us where you kickstart your application?
is it within a jquery document ready function?