我正在使用Marionette
,我遇到以下問題。在Marionette中從一個地區切換到另一個地區,視圖無法正確顯示
我創建了兩個不同區域的佈局。在initialize
佈局在我的佈局的兩個區域加載兩個視圖。說ViewA
和ViewB
。在ViewA
內觸發一個事件。該事件被佈局切換以及其他兩個視圖被注入。說ViewC
和ViewD
。 無論何時執行切換,ViewC
和ViewD
都不具有我應用於它們的相同樣式(也是css)。特別是,jQuery Mobile樣式不適用。有什麼建議?
這裏有一些代碼,其中註釋突出了重要的部分。
onConfirm : function() {
this.leftView = new ViewC();
this.rightView = new ViewD();
this.leftRegion.show(this.leftView);
this.rightRegion.show(this.rightView);
// FIX calling trigger('create') seems fix the problem. Why? Is this correct?
this.$el.trigger('create');
},
initialize : function() {
// listen for event triggered from ViewA
// e.g. GloabalAggregator.vent.trigger("ga:confirm");
// where "ga:confirm" is a simple string
GloabalAggregator.vent.on("ga:confirm" , this.onConfirm, this);
this.leftView = new ViewA(), // creating here a new ViewC the style is applied correctly
this.rightView = new ViewB(); // creating here a new ViewD the style is applied correctly
},
onRender : function() {
this.leftRegion.show(this.leftView);
this.rightRegion.show(this.rightView);
}
編輯
調用trigger('create')
似乎解決這個問題。爲什麼?它是否正確?
此代碼看起來是正確的,除非我錯過了一些東西。你可以在jsFiddle中重現你的問題嗎? –
如果問題出現在樣式中,那麼顯示JS代碼到底是什麼?顯然,CSS層次結構有問題。用Chrome開發者控制檯(或任何其他)檢查你的css,檢查你生成的佈局。沒有看到佈局和css,很難指出真正的問題。但它在那裏 – Tigra
這個代碼絕對不會看起來不正確,除非這是多個視圖的拼湊。通過將視圖分配給同一個變量,即「leftRegion」和「rightRegion」,顯然可以打破區域對象的束縛。不知道這是你的問題,但它肯定是發佈代碼的問題。 –