Permalink
Browse files

Added a note about the add_call method to the design doc.

Fixes #58
  • Loading branch information...
ralphbean committed Nov 6, 2012
1 parent df2920d commit e901b124342b73ad69cf5210fdb9dadd008d4d0a
Showing with 37 additions and 1 deletion.
  1. +37 −1 docs/design.rst
@@ -143,7 +143,6 @@ Non-template Output
Instead of using a template, a widget can also override the ``generate_output`` method. This function generates the HTML output for a widget; by default, it renders the widget's template as described in the previous section, but can be overridden by any function that returns a string of HTML.
Resources
=========
@@ -201,6 +200,43 @@ Toscawidgets2 provides an ``archive_tw2_resources`` distutils command::
.. _middleware:
Constructing Javascript from Python
-----------------------------------
.. https://github.com/toscawidgets/tw2.core/issues/58
If you like, you can invoke Widgets' :meth:`add_call` method inside
:meth:`prepare` to construct dynamic javascript calls at display time.
For instance::
def prepare(self):
super(MyWidget, self).prepare()
# Create a js object for "$(document).ready(.."
when_ready = lambda f: twc.js_function('jQuery')(
twc.js_symbol('document')
).ready(twc.js_callback(f))
# Dicts and other primitives get translated to js properly.
my_js_object = dict(foo="bar", hello="world")
# This is the main function we want to execute
payload = twc.js_function('console.log')(my_js_object)
# Register it all with tw2's middleware for later injection
self.add_call(when_ready(payload))
The above will add the following output to the bottom of the response::
<script type="text/javascript">
jQuery(document).ready(function(){
console.log({"foo": "bar", "hello": "world"})
})
</script>
Middleware
==========

0 comments on commit e901b12

Please sign in to comment.