Mathijs_V
(Mathijs V)
April 16, 2019, 11:17pm
1
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:
Create theme component with code as shown above.
Create a new post, do not refresh, click the calendar icon.
See the alert pop-ups.
Refresh the page. Click on the calendar button again for the same post.
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
sam
(Sam Saffron)
April 17, 2019, 2:05am
2
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
Mathijs_V
(Mathijs V)
April 17, 2019, 7:18am
3
Thanks for your reply! Now I can stop wasting time
Ahh that makes so much sense!
sam:
ask for it on-demand
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
sam
(Sam Saffron)
April 17, 2019, 7:38am
4
Yeah that is pretty much your only option.
4 Likes
Mathijs_V
(Mathijs V)
April 17, 2019, 7:59am
5
Thank you. I will make my workaround less ugly and post it here when finished!
3 Likes
paresy
(Michael)
February 9, 2021, 1:19pm
6
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!
paresy
(Michael)
February 9, 2021, 10:05pm
7
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