# 如何在每次页脚加载（或页面加载？）时触发

**URL:** https://meta.discourse.org/t/how-to-fire-on-every-footer-load-or-page-load/195404
**Category:** Development
**Created:** [2021 年6 月 29 日 19:39 UTC](https://meta.discourse.org/t/how-to-fire-on-every-footer-load-or-page-load/195404 "2021-06-29T19:39:33Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### 作者： ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### 发布日期： [2021 年7 月 3 日 14:28 UTC](https://meta.discourse.org/t/how-to-fire-on-every-footer-load-or-page-load/195404/6 "2021-07-03T14:28:27Z")

</div>

嘿 @Johani，关于“如何触发或被触发”这个问题的另一个版本是：我在一个插件中使用了 [Custom Header Links](https://meta.discourse.org/t/custom-header-links/90588) 的某个版本。它链接到由我的插件添加的独立模型中创建的一些项目（“服务器”）。当创建一个 `server` 时，我希望重新构建头部链接，使其指向最近创建的两个服务器。我现在在一个初始化器中实现这个功能，它运行正常，但只有在添加服务器后刷新页面才能更新。

你之前教过我如何添加和监听触发器，所以我以为也许能解决这个问题，但执行该工作的页面位于 `discourse-subscriptions` 中。也许我想做的是向 `discourse-subscriptions` 提交一个 PR，在其中添加：

```js
this.appEvents.trigger("purchase-complete")

```

在购买完成后（购买操作会触发将用户添加到某个组，进而触发创建服务器并将用户从该组中移除）。或者，如果能在购买完成后，或者在用户点击“购买完成”模态框中的“确定”按钮时直接触发页面刷新，那也很好，但我不知道该如何实现（我之前尝试的方法导致页面无限循环刷新……）

所以，也许可以在这段代码中：

[https://github.com/discourse/discourse-subscriptions/blob/main/assets/javascripts/discourse/controllers/s-show.js.es6#L71-L81](https://github.com/discourse/discourse-subscriptions/blob/main/assets/javascripts/discourse/controllers/s-show.js.es6#L71-L81)

在将 `loading` 设置为 `false` 之后，添加：

```js
this.appEvents.trigger("successful-transaction")

```

然后我可以在我的初始化器中添加：

```js
this.appEvents.on("successful-transaction")

```

来执行头部链接的操作？

我想一旦我这样做，就需要采用不同的方式，因为我担心这段代码：

```ruby
api.decorateWidget("header-buttons:before", (helper) => {
  return helper.h("ul.pfaffmanager-header-links", headerLinks);
});

```

是在 `header-buttons:before` 位置添加内容而不是替换它们，所以每次触发时我都会得到更多的链接？

---

_[View the full topic](https://meta.discourse.org/t/how-to-fire-on-every-footer-load-or-page-load/195404)._
