헤더에 버튼을 삽입하여 사용자를 그룹에 추가하는 기능을 구현했습니다. 동작은 정상적으로 이루어지고 있지만, 이를 위해 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")
);
});
}