我試圖構建一個應用程序,其中有一個處理鼠標事件的CompositeView
,允許您在其中繪製矩形(CompositeView是矩形的集合)。我正在計算/存儲CompositeView
內的矩形(基本上是寬度,高度,頂部,左側和邊框CSS屬性)所需的所有數據。我使用了itemViewOptions
函數,它返回一個包含所有必要數據的對象,它將作爲選項傳遞給我的ItemView
(RectangleView)。在CompositeView中使用Marionette創建一個ItemView
在ItemView
的initialize
方法中,我調用setCssStyle方法,該方法將css屬性應用於視圖。
這裏是我的CompositeView中的(屏幕視圖)和ItemView控件(RectangleView)
var RectangleView = Backbone.Marionette.ItemView.extend({
template: "<div>I am a rectangle</div>",
className: "rectangle",
initialize: function(options){
this.left = options.left;
this.top = options.top;
this.width = options.width;
this.height = options.height;
this.border = options.border;
this.setCssStyle();
},
setCssStyle: function() {
this.$el.css({
'width': this.width + 'px',
'height': this.height + 'px',
'top': this.top + 'px',
'left': this.left + 'px',
'border': this.border
});
}
});
var ScreenView = Backbone.Marionette.CompositeView.extend({
template: "<div> </div>",
className:"screen",
itemView: RectangleView,
itemViewOptions: function() {
return {
left: this.left,
top: this.top,
width: this.width,
height: this.height,
border: this.border
}
},
[...]
});
我讀過,我需要重寫buildItemView
方法傳遞3個參數代碼(中省略爲簡潔的計算和數據stopring方法) :item, ItemViewType, itemViewOptions
根據Marionette Documentation
問題是,我有點困惑,我真的不知道什麼是item
參數我應該通過。它是ItemView的模型嗎?我嘗試了不同的東西,並不斷得到錯誤,所以很可能我錯過了一些基本的東西。
此外,目前我沒有爲我的RectangleView模型。我應該創建一個嗎?如何將itemViewOptions
從我的CompositeView
傳遞到我的ItemView
並最終傳遞給模型?
我提前道歉,如果我沒有解釋我的問題很好,但我的大腦感覺有點肉麻