Retrieving post.raw only works right after creating 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 Likes

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 Like

Yeah that is pretty much your only option.

4 Likes

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

3 Likes

Is this still true? When editing a post the request is to e.g. /posts/13.json

Is there any JS API for that? Or any JS API for making an authenticated request?

@Mathijs_V Would you share your code?

Thanks!

That’s how i did it at the moment:

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

...

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

Taken from here: https://github.com/discourse/discourse-checklist/blob/a94022393c1fd290e492688041896d4392129052/assets/javascripts/discourse/initializers/checklist.js.es6#L39

1 Like