Hi.
When we change page in Discourse, these are the apparent steps:
We click on the link
The current content disappears and the loader appears
The URL is updated and the new content appears
This is slightly different when we go from a page to the home, as the URL is changed to the home URL when the current content disappears, and not when the new content appears.
My goal is to have a script that is executed as soon as we click any page link on Discourse. api.onPageChange won’t fit in this case since the code is executed quite at the same time the new page content is loaded.
Is there a Discourse method for that? I look at the event triggers but didn’t find any one corresponding to what I need.
There’s currently no method in the Plugin-API that will allow you to fire a script before a page transition because this has not come up before as far as I recall.
That said, you can leverage on the willTransition() action in the application route
You would use something that like this in your theme / component
// this fires after the transition
api.onPageChange((url, title) => {
console.log("after transition");
});
// this fires right before the transition
api.modifyClass("route:application", {
pluginId: "some-name",
actions: {
willTransition() {
// run core code first
this._super(...arguments);
// then do some work
console.log("before transition");
// you can also do something like this to see what data you have
// to work with like _router
console.log(this)
}
}
});
وضع الإجراءات في تجزئة (hash) هو صيغة قديمة لم نعد نستخدمها في Discourse. نظريًا يجب أن تعمل، ولكن التوافق مع الصيغة الجديدة قد لا يكون مثاليًا. الطريقة الحديثة ستكون
ينطبق إخلاء المسؤولية القياسي لتعديل الفئة (modifyClass): هذا محفوف بالمخاطر ويمكن أن يتوقف عن العمل في أي وقت
لا أعتقد أن willTransition مضمونة للعمل في مسار التطبيق (application route) في جميع المواقف. (على سبيل المثال، إذا كان للمسار الفرعي إجراء willTransition، فلن ينتقل بالضرورة للأعلى)
أعتقد أنك ستواجه صعوبة في القيام بذلك بشكل مثالي. يتوقع Ember أن يكون له السيطرة الكاملة على العرض، لذا فإن محاولة الربط بنقاط عشوائية ستكون صعبة، وقد تسبب مشاكل (اعتمادًا على ما تحاول القيام به).
من الواضح أنني لا أعرف ما هو هدفك النهائي… لذا ربما يكون الألم مبررًا. في هذه الحالة: لا تتردد في تجاهلي
(بالطبع، إذا كنت ترغب في فتح موضوع Dev حول المشكلة التي تحاول حلها، فسأكون سعيدًا بالاطلاع عليها)
أرى ذلك، لذا فأنت لا تعدل DOM مباشرة
في هذه الحالة، آمل أن تعمل routeWillChange - يجب أن يتم تشغيلها بالتأكيد قبل عرض المسار التالي.
التحذير هو أن… في حالة أشياء مثل عمليات إعادة التوجيه، قد يتم تشغيلها مرتين. أو قد يتم تشغيلها، ثم يتم إلغاء الانتقال في النهاية. ولكن ربما يكون هذا جيدًا في هذه الحالة.