我有一個模式對話框類,我正在使用某些按鈕來顯示錶單。這裏是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"
}
});
由於某些原因解僱事件當我點擊按鈕時不會觸發。這裏發生了什麼?
這是一個好主意。我會試一試。 – Adam
這是2個問題之一。這裏是第2個問題:擴展時,骨幹事件不會從一個視圖繼承到另一個視圖(http://goo.gl/UmdnF)。如果要複製超級事件,則必須在子視圖上重複聲明事件,或者將events屬性聲明爲返回事件哈希的函數,然後在子元素上調用super。例如。 App.AddRecordView.events = function(){return $ .extend(App.ModalView.prototype.events.call(this),{「click .save」:「saveRecord」}); }和App.ModalView.events = function(){return {「click .dismiss」:「dismiss」} – Adam