gpt4 book ai didi

ember.js - 组件之间的 Ember 通信

转载 作者:行者123 更新时间:2023-12-01 09:54:06 25 4
gpt4 key购买 nike

我正在开发一个网络应用程序来显示和搜索文档。我已经像这样布置了主要的包装器 div:

<div class="wrapper">
{{searchComponent searchComponent=searchComponent}}
{{pdfViewer pdfSearchComponent=searchComponent}}
</div>

这允许我稍后在其中添加其他类型的查看器,如下所示:

{{otherViewer otherSearchComponent=searchComponent}}

外包装也是一个 ember 组件。所以它的 Controller 看起来像:

Ember.controller.extend({
searchComponent: null,
.
.
otherProperties,
actions: { }
});

并且搜索组件在初始化时绑定(bind)自身,灵感来自于此来源:http://www.samselikoff.com/blog/getting-ember-components-to-respond-to-actions/

Ember.controller.extend({
searchComponent: null,
.
.
onStart: function(){
this.searchComponent = this;
}.on('init'),
.
.
actions: {
someAction: function() {
//do something
}
}
}

所以我现在可以像这样从主 pdfViewer 引用组件:

this.get('searchComponent').send('someAction')

为了获得响应,我现在将另一个属性绑定(bind)到所有 Controller /模板,然后在查看器 Controller 中观察该属性的变化,之后我可以将结果放在需要的地方。

有没有一种方法可以将“消息”从我的“pdfViewer”发送到我的“searchComponent”并接收​​“响应”,而无需像上面那样将它们显式绑定(bind)在一起?

最佳答案

您可以考虑通过 Service 事件总线使用 pub/sub,您的 searchComponentpdfViewer 都会发出和监听消息,所以可以互相交谈。当然,存在对服务的依赖性,但据我所知,您的组件无论如何都是特定于应用程序的。

类似于:

_listen: function() {
this.get('eventBus').on('message', this, 'handleMessage');
}.on('init'),

actions: {
click() { this.get('eventBus').trigger('message', message); }
}

几周前,我评估了几种父子组件通信的方法:http://emberigniter.com/parent-to-children-component-communication/ ,也许这会有所帮助。

关于ember.js - 组件之间的 Ember 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32210120/

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