你好,我也遇到过同样的问题,通过重新获取主题来获取所需数据解决了。我在 topic-list-item.js 组件中使用了 Ember.PromiseProxyMixin 来实现。如何使用它,请阅读这里:Use Ember.PromiseProxyMixin in a theme
代码大致如下:
// 我使用了自定义的 memoize 函数来减少请求次数
const getTopic = memoize(id => ajax(`/t/${id}.json`).then(data => data));
// ...
// 在组件内部
@discourseComputed("topic")
topicPromise(topic) {
return getTopic(topic.id);
},
@discourseComputed("topicPromise")
topicProxy() {
const promise = this.get("topicPromise");
return promise && PromiseObject.create({ promise });
},
postStream: reads("topicProxy.content.post_stream"),
@discourseComputed("postStream")
excerpt(postStream) {
if (!this.get("postStream")) return false;
return `${stripHtml(postStream.posts[0].cooked).slice(0, 150)}...`;
},
stripHtml 是一个自定义函数,用于移除首帖中的 HTML。
在 topic-list-item.hbs 中,你可以这样写:
{{#if topicProxy.isFulfilled}}
<div class="excerpt">
{{#if excerpt}}
{{html-safe excerpt}}
{{/if}}
</div>
{{/if}}