2013-02-11 47 views
1

如何檢查插座是否已連接到我的應用程序模板?在renderTemplate()我想檢查插座是否需要連接或不連接(出於性能原因)。最終的代碼看起來應該與此類似:Ember Pre4:如何檢查插座是否已連接?

renderTemplate: function(controller, model) { 
    var isMyOutletConnected = //how to do that? 

    if(!isMyOutletConnected){ 
     this.render('someTemplate', { // the template to render 
      into: 'application',   // the template to render into 
      outlet: 'someOutlet',  // the name of the outlet in that template 
      controller: "someController" // the controller to use for the template 
     }); 
    } 
} 

我試圖用容器來查找通過應用程序視圖:container.lookup("view:application) 但這實例化一個新的觀點,而不是返回現有之一。

+0

Ember已經爲你做到這一點。如果您轉換到相同的視圖,它不會重新呈現視圖。它也不會創建另一個控制器,因爲控制器的行爲與單例相似。如果是性能問題,那麼您應該保持良好狀態,因爲在添加支票時,您實際上正在增加應用程序在轉換時不得不做的工作。 – Wildhoney 2013-02-11 17:42:50

+0

實際上並非如此。我發佈的解決方案使我大大提高了性能。如果你看一下render()的代碼,你會發現,這個代碼總是涉及到完全拆除現有插座,儘管沒有任何改變。這可以通過其他方式得到緩解,但檢查現有網點是一種可能的方法。 – mavilein 2013-02-12 10:59:05

回答

0

感謝您的輸入。這是我想出的解決方案:

1 - 爲我的單例視圖創建視圖註冊表。查看註冊表駐留在Application實例中,其屬性由didInsertElement中的視圖設置。

var App = Ember.Application.create({ 
    viewRegistry : { 
     applicationView : null 
    } 
}); 

App.ApplicationView = Ember.View.extend({ 
    templateName : 'application', 
    didInsertElement : function(){ 
     App.set("viewRegistry.applicationView", this); 
    } 
}); 

2 - 現在我可以訪問此註冊表在我的路線檢查連接網點

isOutletOfApplicationViewConnected : function(outletName){ 
    var applicationView = App.viewRegistry.applicationView; 
    if(applicationView){ 
     return applicationView.get("_outlets." + outletName) != undefined; 
    }else{ 
     return false; 
    } 
}, 
renderTemplate: function(controller, model) { 
    var isMyOutletConnected = this.isOutletOfApplicationViewConnected("someOutlet"); 

    if(!isMyOutletConnected){ 
     this.render('someTemplate', { // the template to render 
      into: 'application',   // the template to render into 
      outlet: 'someOutlet',  // the name of the outlet in that template 
      controller: "someController" // the controller to use for the template 
     }); 
    } 
} 

這種解決方案可以更加通用,因爲該方法「isOutletOfApplicationViewConnected」硬連線到我的應用程序視圖,但這是一個很好的開始,適用於我。

0

使用Jquery插件livequery註冊一個回調,當你的元素插入到DOM。這可以在視圖中完成自身

this question