将组件添加到现有Ractive实例

时间:2016-01-26 08:26:19

标签: ractivejs

我有一个奇怪的问题。

在Ractive,我们可以做这样的事情。

应用模板:

<h1>My app</h1><SubComponent/>

通常SubComponent来自App的{​​{1}}模板访问数据。

如果没有在模板中提及SubComponent,有没有办法获得相同的行为?

类似:

const App = new Ractive({el: ‘#myapp’, …………});
const SubComponent = Ractive.extend({ append: true, ………… });
const example = new SubComponent()
example.render(App.el)

但是,example的模板可以访问App的数据,也许App有机会使用App.findComponent()找到它。

我正在尝试做的是,具有不可预测性(=我不能将它们默认包含在模板中)嵌套组件。

希望这是有道理的。

关于如何做的任何想法?

1 个答案:

答案 0 :(得分:0)

我不知道这对你有用吗? 但是你可以在

中放入一个已知ID的div
<h1>My app</h1><div id="subcomp" />

然后在那里安装动态SubComponent。 您可以使用像oncomplete这样的can事件 - 这将保留比div#subcomp已添加到dom中。

oncomplete: function() {
   this.mycompRef = new SubComponent( { el: '#subcomp' });
   // You can skip findComponent and use mycompRef
}