الوصول إلى نموذج آخر في Ember - بالتأكيد هناك طريقة أفضل من ajax

I’m inserting a button on the header that adds a user to a group. This is working, but making an ajax call to do so just doesn’t seem right. It would seem like I could “just” somehow call the Group model and do it there, but I can’t find an example that does it.

This is in a plugin…

I guess there must be some way that I can make the demoGroup into a model rather than just a 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")
          );
        });
      }
إعجاب واحد (1)

If the object you’re dealing with is a Group model object, you can direct call addMembers on it. If not you can use Group.findAll method.

For usage, lookup the codebase.

إعجاب واحد (1)

Hmm. Looking more closely at what groups.js does, it looks like it’s making pretty much the same json call that I am, and worse, the Group.findAll() that it appears I would need to do in order to be able to use Group.addMembers adds an extra ajax call that I’m currently avoiding. Maybe my code isn’t as bad as I thought!

Thanks!

إعجاب واحد (1)