Deleting a Reddit Post
Delete
When reviewing the Reddit API documentation, how the server identifies what to delete is through it's 'fullname' - sent as 'id'. The fullname of an object is the combination of a thing's type such as link, or selfpost, appreviated to 't*_', along with the id of the object itself. for example: t3_fh7si is the fullname for a selfpost with the id of fh7si.
Another issue I faced originally when getting this function to be called in the first place is that the delete button would refresh every time I pressed it.
This took me so long to figure out, but It's because the button is dynamically generated. As the button is dynamically generated, and the JQuery script loads once the DOM is ready, technically there is no such thing as the class 'deleteButton'. This was an issue as I was using JQuery's click() method. The solution to this is to use the more versatile .on("click", "[object]", function() ) as seen above. This solution worked perfectly. I found this Stack Overflow question helped: https://stackoverflow.com/questions/15090942/event-handler-not-working-on-dynamic-content.
The final issue I faced with this solution was determining what delete button was being pressed, and in relation to what post needs deleting. My thought process was to send a form with the ID in a hidden form element, but this would be pretty inefficient so I ditched this idea. The solution to this was relatively simple, and did not take nearly as much code. If the id of the post was dynamically added to the id element of the button, it could be accessed though 'event.target.id'. This solved the issue.
Comments
Post a Comment