У меня есть модальный класс диалога, который я расширяю, чтобы показать форму с некоторыми кнопками. Вот вид ModalView:Backbone 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();
}
});
Вот расширенный вид:
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"
}
});
По какой-либо причине событие отклонения никогда не срабатывает, когда я нажимаю кнопку. Что тут происходит?
Это отличная идея. Я дам ему попробовать. – Adam
Это была одна из 2-х проблем. Вот проблема номер 2: Магистральные события не наследуются от одного вида к другому при расширении (http://goo.gl/UmdnF). Если вы хотите реплицировать события super, вам нужно либо объявить события в двух экземплярах на дочернем представлении, либо объявить атрибут событий функцией, которая возвращает хеш событий, а затем вызвать супер над дочерним элементом. Например. App.AddRecordView.events = function() {return $ .extend (App.ModalView.prototype.events.call (this), {"click .save": "saveRecord"}); } и App.ModalView.events = function() {return {"click .dismiss": "увольнять"} – Adam