[英]Ember Pre4: How to check if an outlet is already connected?
如何檢查應用程序模板中是否已連接插座? 在renderTemplate()中,我想檢查是否實際需要連接插座(出於性能原因)。 最終代碼應類似於以下內容:
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)
但這實例化了一個新視圖,而不是返回現有視圖。
將元素插入dom時,使用Jquery
插件livequery
注冊回調。 這可以在視圖本身中完成
看到這個問題
感謝您的輸入。 這是我想出的解決方案:
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”已硬連線到我的應用程序視圖,但這是一個不錯的開始,對我來說很有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.