Add utility for sending wrapped things in js_preload.

This commit is contained in:
Max Goodman
2013-03-22 00:35:31 -07:00
parent 90dc3e877b
commit 2c4553fd24
2 changed files with 17 additions and 0 deletions

View File

@@ -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

View File

@@ -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)