diff --git a/r2/r2/lib/pages/things.py b/r2/r2/lib/pages/things.py index 2f9a67b12..bf347802b 100644 --- a/r2/r2/lib/pages/things.py +++ b/r2/r2/lib/pages/things.py @@ -232,3 +232,14 @@ def wrap_links(links, wrapper = default_thing_wrapper(), return l.listing() +def wrap_things(*things): + """Instantiate Wrapped for each thing, calling add_props if available.""" + if not things: + return [] + + wrapped = [Wrapped(thing) for thing in things] + if hasattr(things[0], 'add_props'): + # assume all things are of the same type and use the first thing's + # add_props to process the list. + things[0].add_props(c.user, wrapped) + return wrapped diff --git a/r2/r2/lib/template_helpers.py b/r2/r2/lib/template_helpers.py index 65c0acafe..f2c8d44ef 100755 --- a/r2/r2/lib/template_helpers.py +++ b/r2/r2/lib/template_helpers.py @@ -168,6 +168,12 @@ class JSPreload(js.DataSource): def set(self, url, data): self.data[url] = data + def set_wrapped(self, url, wrapped): + from r2.lib.pages.things import wrap_things + if not isinstance(wrapped, Wrapped): + wrapped = wrap_things(wrapped)[0] + self.data[url] = wrapped.render_nocache('', style='api').finalize() + def use(self): hooks.get_hook("js_preload.use").call(js_preload=self)