Certain plugins need the ability to “preload” data in the topic list.
Some specific examples:
- Discourse solved adds an icon on the topic list when a topic is solved
- Discourse assign will display the username the topic is assigned to on the topic list
To achieve that we have 2 mechanisms:
Preloading a custom field
If you have any custom fields you need for rendering of the topic list use:
TopicList.preloaded_custom_fields << "my_custom_field"
This will force the system to ensure that the Topic object model has the custom field loaded when serializing.
If you do not do that and try to access a custom field an error will be raised.
###Preloading custom data
In some cases you may need to preload custom data that goes beyond a custom field. To do so:
TopicList.on_preload do |topics, topic_list| # your custom code to load up all topic data in bulk goes here end
This is useful when you need to reach out and preload other models.
By carefully using both these methods you can ensure your plugin does not cause N+1 queries.