post.raw の取得は、post 作成直後でのみ正常に機能する

Hi there,

At the moment I am working on an extra PostMenuButton. Funny thing is that the Cooked post is always available but the Raw post is not!

Running discourse version [v2.3.0.beta8 +103]

<script type = "text/discourse-plugin" version = "0.1" >

  api.addPostMenuButton('trimetrack', attrs => {
    return {
      action: 'createTimeTracking',
      icon: 'calendar-alt',
      className: 'time-tracking',
      title: 'time_tracking',
      position: 'first'
    };
  }
                       );
  api.attachWidgetAction('post-menu', 'createTimeTracking', function() {
    const post = this.findAncestorModel();
    alert("Raw post: " + post.raw);
    alert("Cooked post: " + post.cooked);
  });
  
  </script>

Steps to reproduce:

  1. Create theme component with code as shown above.

  2. Create a new post, do not refresh, click the calendar icon.

  3. See the alert pop-ups.
    image

  4. Refresh the page. Click on the calendar button again for the same post.
    image

  5. Now the post only has the cooked content available, raw content is not getting through.

I hope anyone can either spot my failure or confirm the bug :slight_smile:

This is by design, we prefer to keep our topic payload smaller so if you need raw you have to ask for it on-demand.

Otherwise we would be shipping data to all clients that they do not need the vast majority of the time.

「いいね!」 4

Thanks for your reply! Now I can stop wasting time :slight_smile:

Ahh that makes so much sense!

Could you point me in the right direction to retrieve post data from javascript?
I had a sort of workaround to get the .json by retrieving the /t/raw/{topicid}/{postid}.json but that was really ugly and there must be a better way.

「いいね!」 1

Yeah that is pretty much your only option.

「いいね!」 4

Thank you. I will make my workaround less ugly and post it here when finished!

「いいね!」 3

これはまだ事実ですか?投稿を編集する際、リクエストは例えば /posts/13.json のように送られます。

それに対する JS API は存在しますか?あるいは、認証付きリクエストを行うための JS API はありますか?

@Mathijs_V さんのコードを共有していただけませんか?

ありがとうございます!

現在の私の実装方法は以下の通りです:

import {ajax} from "discourse/lib/ajax";

...

let post = this.findAncestorModel();
ajax(`/posts/${post.id}`, { type: "GET", cache: false }).then(
  (result) => {
    console.log(result.raw);
  }
);

出典: discourse-checklist/assets/javascripts/discourse/initializers/checklist.js.es6 at a94022393c1fd290e492688041896d4392129052 · discourse/discourse-checklist · GitHub

「いいね!」 1