أحاول تحميل نموذج JavaScript في التذييل.
لدي هذا في الرأس:
<script type="text/discourse-plugin" version="0.8">
let loadScript = require("discourse/lib/load-script").default;
api.onPageChange(() => {
loadScript("//js.hsforms.net/forms/current.js").then(() => {
console.log("doing the thing");
hbspt.forms.create({
portalId: "229276",
formId: "a86ca9cc",
submitButtonClass: "button orange-button hubspot-button",
target: ".subscription-form"
});
});
});
</script>
وهذا في التذييل:
<div class="subscription-form clearfix">
<h5>اشترك في مدونتنا</h5>
</div>
في المرة الأولى التي يتم فيها تحميل الصفحة، يعمل كل شيء بشكل صحيح. أما الصفحات اللاحقة (معظمها على الأقل)، فإنها تفشل في تحميل النموذج وتظهر رسالة خطأ:
Couldn't find target container .subscription-form for HubSpot Form a86ca9cc. Not rendering form onto the page
أعتقد أن التذييل ربما يتم تحميله بعد تنفيذ السكربت؟
لذلك أحتاج بطريقة ما إلى تأخير تنفيذ السكربت حتى يتم تحميل التذييل.
يُظهر ملف plugin-api.js ما يلي:
// Listen for a triggered `AppEvent` from Discourse.
api.onAppEvent("inserted-custom-html", () => {
console.log("a custom footer was rendered");
});
ربما أحتاج فقط إلى معرفة أي AppEvent يجب البحث عنه؟