Decorate topic list with jquery?


#1

I understand that there is a way to decorate posts after cooking with api.decorateCooked(). Is there a similar method for decorating topic list items or the topic list?


(Régis Hanol) #2

What are you trying to achieve?


#3

I’m making a plugin that incidentally interacts with Topic List Previews.

My end goal is to display these http://bl.ocks.org/ in a post (which I have working), but I also want to display the thumbnail in the topic-item. I overrode ExcerptParser.rb methods to allow my custom tag to go through (akin to the spoiler class attribute on div and span). My hope is to grab the data within the allowed span tag and render the thumbnail after it uses this data to make an ajax request.


(Angus McLeod) #4

Could you break this down a bit more into discrete steps? I assume you’ve looked at how TLP creates and uses thumbnails in the topic list?


#5

Yes I have indeed looked over the TLP code. Unfortunately, particular to how I render the blocks (for example Choropleth - bl.ocks.org), the only cooked data that resides in the database is an html tag,

<div class="gist" data-user="mbostock" data-gist="4060606"></div>

For decorating posts, I’m currently using jquery to find the .gist class, get() the gist data according to the user and gist, then render the iframe.

For excerpts, I was hoping to use a similar method, using the untouched div in the excerpt to get the data necessary to find the thumbnail on github.


(Angus McLeod) #6

I reckon the best way to achieve what you’re trying to do is add a method that listens for the post created event on the server. See a similar method in my ratings plugin here.

In this method you should

  1. Parse the cooked html for what you need.

  2. Use the parsed content to get your data.

  3. Save the response to a custom field.

Then you’ll add a new method to the topic list item serializer that gets your saved data from the custom field.

Then you just need to render the serialized data in the topic list item using one of the plugin outlets.

I would avoid making an ajax request on the client if possible.


#7

Your suggestion is quite excellent. I will use your approach when parsing for the thumbnail info.

I will admit, using ajax in contexts like this is a janky work around. :sweat_smile: