# Including composer custom fields on save draft

**URL:** https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913
**Category:** Development
**Created:** [8월 27, 2019, 9:12오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913 "2019-08-27T09:12:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [8월 27, 2019, 9:12오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/1 "2019-08-27T09:12:00Z")

</div>

Recently, I started looking into a bug with the Events plugin where the events data shown on the composer is lost if the post is saved to drafts.

I tried to fix the issue and soon found out that there’s no API exposed by Discourse which allows us to do that.

I think this is an important feature from a UX standpoint with many potential use cases other than what I’m faced with.

For that, I am making a PR, which fixes this issue by introducing a new method in the Composer Model called `serializeToDraft`. This method will add those custom fields while saving the post to draft. Also, those fields will be set to the composer model when the draft is reopened.

@angus is helping me out on this PR by reviewing the code and suggesting the important improvements to be made.

I would like to know the thoughts of the Discourse team on this feature.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [8월 29, 2019, 3:39오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/2 "2019-08-29T03:39:35Z")

</div>

I agree we should have a clean mechanism for plugins to add and retrieve draft info associated with a post.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [10월 9, 2019, 7:42오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/3 "2019-10-09T07:42:22Z")

</div>

저와 @angus가 이 문제를 해결하기 위해 PR을 만들었습니다.

> <https://github.com/discourse/discourse/pull/8175>
>
> This PR adds a function \`serializeToDraft\` to the composer model to allow saving… topic custom fields to draft and setting them back when the draft is reopened.
> 
> Here is the post on meta for this.
> https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/2

---

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [11월 23, 2019, 4:57오후 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/4 "2019-11-23T16:57:44Z")

</div>

몇 가지 이해되지 않는 부분이 있습니다: 왜 `serializeToDraft`는 플러그인 API의 일부인 반면, `serializeOnCreate`와 `serializeToTopic`은 그렇지 않나요? 나머지 두 메서드가 없는 상황에서 `serializeToDraft`가 유용한 경우는 언제이며, 그 반대는 어떨까요? 포스트/토픽과 초안 모두에서 직렬화를 수행하는 래퍼가 있어야 하지 않나요?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [11월 24, 2019, 11:37오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/5 "2019-11-24T11:37:14Z")

</div>

네, 동의합니다. 그것들도 PR을 올려주세요.

---

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [1월 16, 2020, 10:44오전 UTC](https://meta.discourse.org/t/including-composer-custom-fields-on-save-draft/126913/6 "2020-01-16T10:44:04Z")

</div>

또 다른 문제를 발견했습니다: saveDraft() 메서드는 어떻게 처리해야 하나요? serializeToDraft()로 추가된 필드에 대해 이 링크와 유사한 옵저버가 필요합니다:

> <https://github.com/discourse/discourse/blob/c01c937151526f5ab55ecfe7a5e8f84c9081f05b/app/assets/javascripts/discourse/controllers/composer.js.es6#L1073>

그렇지 않으면 커스텀 초안 필드만 변경된 경우 초안이 엔드포인트로 전송되지 않습니다.  
따라서 saveDraft()도 API의 일부가 되어야 하거나, 실제로는 이 부분이 어떻게든 추상화되어야 합니다.  
또한 composermodel에서 dataChanged 옵저버를 발견했습니다. 같은 것을 감시하는 두 개의 옵저버가 있다는 것은 매우 이상하다고 생각합니다. 이 옵저버 내부의 로직은 커스텀 초안 필드에 대해서도 트리거되어야 할 가능성이 높습니다. 또한 두 옵저버 중 어느 쪽이 먼저 실행되는지, 그리고 그 영향이 무엇인지도 궁금합니다.

> <https://github.com/discourse/discourse/blob/eef21625c6b9dd589573bc1771c7075a12e0abc9/app/assets/javascripts/discourse/models/composer.js.es6#L1162>
