2013-07-31 34 views
2

我正在使用Marionette,我遇到以下問題。在Marionette中從一個地區切換到另一個地區,視圖無法正確顯示

我創建了兩個不同區域的佈局。在initialize佈局在我的佈局的兩個區域加載兩個視圖。說ViewAViewB。在ViewA內觸發一個事件。該事件被佈局切換以及其他兩個視圖被注入。說ViewCViewD。 無論何時執行切換,ViewCViewD都不具有我應用於它們的相同樣式(也是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')似乎解決這個問題。爲什麼?它是否正確?

+0

此代碼看起來是正確的,除非我錯過了一些東西。你可以在jsFiddle中重現你的問題嗎? –

+0

如果問題出現在樣式中,那麼顯示JS代碼到底是什麼?顯然,CSS層次結構有問題。用Chrome開發者控制檯(或任何其他)檢查你的css,檢查你生成的佈局。沒有看到佈局和css,很難指出真正的問題。但它在那裏 – Tigra

+0

這個代碼絕對不會看起來不正確,除非這是多個視圖的拼湊。通過將視圖分配給同一個變量,即「leftRegion」和「rightRegion」,顯然可以打破區域對象的束縛。不知道這是你的問題,但它肯定是發佈代碼的問題。 –

回答

1

當你添加新的頁面到頁面(添加新的渲染視圖,交換視圖的一些其他視圖等),你必須讓jQuery手機知道這一點。

「create」事件用於增強jQuery Mobile小部件的「raw」html。更多信息可以在這裏找到:http://jquerymobile.com/demos/1.0/docs/pages/page-scripting.html 尋找「增強新標記」部分。

+0

這裏很好。謝謝。 –

相關問題