Ignore:
Timestamp:
23 Jul 2009, 00:49:50 (15 years ago)
Author:
uli
Message:

Extend widget tests. Test coverage for waeup.widgets now 100%.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/widgets/table.txt

    r4405 r4420  
    3636    mytable
    3737
     38YUITable
     39--------
     40
     41A YUI table extends a regular table by adding some JavaScript
     42code-generation facilities suitable for rendering YUI based
     43datatables. Futhermore they provide a `need` method, which includes
     44all needed YUI scripts and stylesheets.
     45
     46
     47    >>> from waeup.widgets.table import YUITable
     48    >>> table = YUITable('My YUI Table', cols=(id, mycol1, mycol2))
     49    >>> print table.getJSTableCode()
     50    <BLANKLINE>
     51    YAHOO.util.Event.addListener(window, "load", function() {...
     52    ...
     53    draggableColumns:true });
     54    });
     55
     56Naturally, YUITable objects are suited for use by views.
     57
     58
     59YUIStaticTableView
     60==================
     61
     62This is a view named ``yuistatictables.js`` which from a users'
     63perspective can be used like a static file. It is, however, not
     64static, but returns the JavaScript code suitable for a given table.
     65
     66We define some content object that contains a table for which we want
     67the JavaScript to be generated:
     68
     69    >>> class SomeContent(object):
     70    ...   mytable = table
     71
     72    >>> obj = SomeContent()
     73
     74Now we need an adapter, that knows how to get a table from this kind
     75of object. The YUIStaticTableView looks up such an adapter for a given
     76context to find a table to manage. The adapter has to implement
     77`ITableProvider` from `waeup.widgets.interfaces` and should provide a
     78`getTable` method.
     79
     80We define and register such an adapter using grok:
     81
     82    >>> import grok
     83    >>> grok.testing.grok('waeup')
     84    >>> from waeup.widgets.interfaces import ITableProvider
     85    >>> class TableAdapter(grok.Adapter):
     86    ...   grok.context(SomeContent)
     87    ...   grok.provides(ITableProvider)
     88    ...
     89    ...   def getTable(self):
     90    ...     # We know that our context stores a table in `mytable`
     91    ...     return self.context.mytable
     92    >>> grok.testing.grok_component('TableAdapter', TableAdapter)
     93    True
     94
     95Now let's get a view on the object defined above. We want the view
     96named ``yuistatictables.js``:
     97
     98    >>> from zope.publisher.browser import TestRequest
     99    >>> from zope.component import getMultiAdapter
     100    >>> from zope.interface import Interface
     101    >>> view = getMultiAdapter((obj, TestRequest()), Interface,
     102    ...                        name='yuistatictables.js')
     103    >>> view
     104    <waeup.widgets.table.YUIStaticTableView object at 0x...>
     105
     106When we call the view we get pure JavaScript:
     107
     108    >>> print view()
     109    <BLANKLINE>
     110    YAHOO.util.Event.addListener(window, "load", function() {
     111    ...
     112    draggableColumns:true });
     113    });
     114
     115Table views for objects that have no table
     116------------------------------------------
     117
     118What happens, when no `ITableProvider` can be found for an object? The
     119view will return an empty string.
     120
     121Let's try this with a simple object for which no adapter is
     122registered. We can get the view:
     123
     124    >>> obj = object()
     125    >>> view = getMultiAdapter((obj, TestRequest()), Interface,
     126    ...                        name='yuistatictables.js')
     127    >>> view
     128    <waeup.widgets.table.YUIStaticTableView object at 0x...>
     129
     130But if we call this adapter, we will get an empty result, because
     131there is no ITableProvider adapter registered for the 'viewed' object:
     132
     133    >>> view()
     134    ''
Note: See TracChangeset for help on using the changeset viewer.