האם אפשר להעביר פרמטרים ל-API שיוצר קישורי קהילה?

סיכום מבוסס בינה מלאכותית

The discussion revolves around passing parameters to the API that creates Community Links in Discourse. merefield is trying to add a community link with a route that requires a tag_id parameter, but is having trouble passing the parameter. awesomerobot suggests adding a get models() method to the link, which merefield implements, but still encounters issues with a missing postStream upon clicking the link. awesomerobot provides an example of how to use the get models() method, which helps merefield resolve the issue. The conversation also touches on a pull request by awesomerobot that aims to improve the API. The name ‘models’ for the query params is noted to be not entirely intuitive, as it is related to Ember’s LinkTo component expecting a model argument, as explained by awesomerobot.

לדוגמה,

       api.addCommunitySectionLink({
         name: "intersection-navigator",
         route:"tags.intersection",
         title: I18n.t("tag_intersection_navigator.link.title"),
         text: I18n.t("tag_intersection_navigator.link.text")
       })

… ייכשל מכיוון שמסלול זה דורש tag_id.

ניסיתי להוסיף:

        params: { tag_id:  siteSettings.discourse_tag_intersection_navigator_all_word },
        model: { tag_id: siteSettings.discourse_tag_intersection_navigator_all_word },

… אבל אני חושד מאוד שזה לא מעובד כנדרש על ידי ה-API …

Ah, I’ve made progress!

It involves this:

Specifically, I think it’s here: discourse/app/assets/javascripts/discourse/app/lib/sidebar/custom-community-section-links.js at d0c3f3b8fe6905e7e33ae4944cdf44c11ccc0df6 · discourse/discourse · GitHub

if we add this it should work:

get models() {
 return args.models;
}

https://github.com/discourse/discourse/pull/33070

תודה רבה שפתחת!

אה, ומכיוון שיש תנאי typeof args === "function", אתה יכול לעשות משהו כזה באופן מיידי:

api.addCommunitySectionLink((Base) =>
  class CustomLink extends Base {
    get name() {
      return "intersection-navigator";
    }

    get route() {
      return "tag.show";
    }

    get models() {
      return ["tagid"];
    }

    get title() {
      return I18n.t("tag_intersection_navigator.link.title");
    }

    get text() {
      return I18n.t("tag_intersection_navigator.link.text");
    }

  }
);

כן, זה מה שאני כבר עושה… אבל יש קטע מצחיק בהמשך התהליך

משום מה, זה לא ממש זה ואני מקבל תלונה על חוסר postStream בעת לחיצה:

api.addCommunitySectionLink((baseSectionLink) => {
        return class CustomSectionLink extends baseSectionLink {
          get name() {
            return "intersection-navigator";
          }

          get route() {
            return "tags.intersection";
          }

          get models() {
            return [{tag_id: siteSettings.discourse_tag_intersection_navigator_all_word}];
          }

          get title() {
            return I18n.t("tag_intersection_navigator.link.title")
          }

          get text() {
            return I18n.t("tag_intersection_navigator.link.text");
          }
        };
      });

זה כמעט כאילו שאתה צריך לכלול את המודל עבור המסלול הנוכחי, וזה מוזר!

מה הפלט של ההגדרה הזו? נראה שהיא עובדת בסדר כשאני עושה את זה ידנית:

get models() {
  return ["featured", "tv"];
}

כן, זה עובד, זה ממש מועיל, תודה!

ההגדרה היא רק מחרוזת. אני חושב שחשבתי על זה יותר מדי.

השם “models” עבור פרמטרי השאילתה הוא לא אינטואיטיבי ב-100%?

יחסי הציבור שלך יהפכו את זה להרבה יותר מסודר, תודה!

yeah it wasn’t clear to me at first either, but it’s because in the end it uses Ember’s LinkTo component, which expects a model argument…