2013-08-27 49 views
3

我想以編程方式將不同模板呈現爲基於模型中特定值的命名插口。Ember JS根據模型中的內容動態呈現模板

下面是兩個例子JSBin:

這一個示出了基本結構,但具體的呈現代碼註釋。 http://jsbin.com/OhegexO/1/

但是當我嘗試使用我的路由renderTemplate方法不起作用

http://jsbin.com/OhegexO/2/

我看到在我的控制檯下面的錯誤

Error while loading route: TypeError {} 

Uncaught TypeError: Cannot call method 'connectOutlet' of undefined 

我似乎弄清楚這一點。用例是我想根據模型中的某些參數使用不同的模板。

回答

3

不可否認,我並不是很熟悉渲染到指定插座,但它似乎不會渲染到尚未渲染的插座中。也就是說,可以不用renderTemplate來完成它,你可以允許產品模板正常渲染(不要覆蓋renderTemplate),然後渲染編輯窗體,然後將其放入產品模板中(請參閱2nd jsbin)。這有點尷尬,所以如果你想劫持renderTemplate爲了它,看到這第一個jsbin。它們都涉及等待產品模板呈現,然後呈現它。

我相信如果您重寫renderTemplate掛鉤,它將跳過默認呈現,因此不會呈現產品模板。經過進一步調查,這是真的,如果你超級,它也可以。順便說一句this._super()也說運行這個方法的默認實現。

http://jsbin.com/ujiKire/5/edit

使用設定控制器:

http://jsbin.com/OvONejo/1/edit

setupController: function(controller, model){ 
    var templateEditForm = model.get('editform'); 
    var templateInto = 'product'; 
    var templateOutlet = 'editform'; 

    console.log("render the [%s] form into the [%s] template, using the [%s] outlet",templateEditForm, templateInto, templateOutlet);  

    // Why does this code not work 

    var self = this; 
    Ember.run.later(function(){ 
    self.render(templateEditForm, { // the template to render 
     into: 'product',    // the template to render into 
     outlet: templateOutlet,    // the name of the outlet in that template 
     controller: controller  // the controller to use for the template 
    }); 
    },1); 
    } 
+0

哇哦,謝謝你,謝謝你,謝謝你!我從來沒有猜到過。在該運行循環中,本質上是說「在1毫秒後執行此操作」。有趣。 –