Ember에서 다른 모델 접근하기 – ajax보다 나은 방법이 분명 있을 텐데

헤더에 버튼을 삽입하여 사용자를 그룹에 추가하는 기능을 구현했습니다. 동작은 정상적으로 이루어지고 있지만, 이를 위해 ajax 호출을 사용하는 방식이 이상하게 느껴집니다. 단순히 Group 모델을 호출하여 해당 작업을 수행할 수 있을 것 같은데, 그렇게 하는 예제를 찾지 못하고 있습니다.

이것은 플러그인에서 작성된 코드입니다…

아마도 demoGroup을 단순히 dict가 아닌 모델로 만들 수 있는 방법이 있을 것 같은데?

      const demoServerGroupName = siteSettings.pfaffmanager_demo_server_group;
      const groups = Site.currentProp("groups");
      const router = container.lookup("router:main");

      const demoGroup = groups.find((g) => {
        return g.name === demoServerGroupName;
      });
      const hasDemoServer = servers.find((g) => {
        return g.hostname.includes("pfaffmanagerdemo");
      });
      if (demoGroup && !hasDemoServer) {
        api.decorateWidget("header-buttons:before", (helper) => {
          const addToDemoGroup = function () {
            window.console.log("need to add to group", demoGroup);

            const groupId = demoGroup.id;
            const url = `/groups/${groupId}/join.json`;
            ajax(url, {
              type: "PUT",
              data: currentUser.username,
            })
              .then(() => {
                router.transitionTo("/pfaffmanager/servers"); //TODO: refer to the route?
              })
              .catch(popupAjaxError);
          };

          return helper.h(
            "button#newDemoServer",
            {
              className: "btn btn-text btn-primary create headerLink",
              title: I18n.t("pfaffmanager.server.create_demo_server_title"),
              onclick: addToDemoGroup,
            },
            I18n.t("pfaffmanager.server.create_demo_server_short")
          );
        });
      }

다루고 있는 객체가 Group 모델 객체인 경우, addMembers를 직접 호출할 수 있습니다. 그렇지 않은 경우 Group.findAll 메서드를 사용할 수 있습니다.

사용법에 대해서는 코드베이스를 참고하세요.

음. groups.js가 실제로 무엇을 하는지 좀 더 자세히 살펴보니, 제가 하고 있는 것과 거의 동일한 JSON 호출을 하고 있는 것 같습니다. 게다가 Group.addMembers를 사용하려면 Group.findAll()을 호출해야 할 것 같은데, 이렇게 하면 제가 현재 피하고 있는 추가 ajax 호출이 발생합니다. 아마 제 코드가 생각했던 것만큼 나쁘지 않은 것 같습니다!

감사합니다!