gpt4 book ai didi

ember.js - Ember JS 根据模型中的内容动态渲染模板

转载 作者:行者123 更新时间:2023-12-02 00:05:49 29 4
gpt4 key购买 nike

我正在尝试根据模型中的特定值以编程方式将不同的模板渲染到命名 socket 中。

这里有两个 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

我似乎可以解决这个问题。用例是我想根据模型中的某些参数使用不同的模板。

最佳答案

诚然,我对渲染到指定的 socket 不是很熟悉,但它似乎不会渲染到尚未渲染的 socket 。话虽这么说,而不是从 renderTemplate 进行操作,您可以允许产品模板正常呈现(不要覆盖 renderTemplate),然后将编辑表单延迟呈现到产品模板中(参见第二个 jsbin)。这有点尴尬,所以如果您想为此劫持 renderTemplate,请参阅第一个 jsbin。它们都涉及等待产品模板呈现,然后呈现它。

而且我相信如果您覆盖 renderTemplate Hook ,它会跳过默认呈现,因此永远不会呈现产品模板。进一步调查,确实如此,如果你 super 它,它也有效。顺便说一句,this._super() 表示还要运行此方法的默认实现。

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

使用设置 Controller :

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);
}

关于ember.js - Ember JS 根据模型中的内容动态渲染模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455552/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com