===================== Azax views and events ===================== When we make a change using Ajax calls we would like to update the browser page. This can be done by sending browser commands. Some part of the page may need to be update based on modifications. We don't want to handle all these modifications in our own code. An example of such a modification change would be changing the title. This should also update the navigation portlet. To make this work azax views do something special. We will explain this by creating an example. The following will setup our enviroment and show that normal operation works. >>> from kss.core.azaxview import AzaxBaseView >>> from kss.core.interfaces import IAzaxEvent >>> from kss.core.tests.base import IDebugRequest >>> from zope import component >>> from zope.lifecycleevent import ObjectModifiedEvent >>> from zope.lifecycleevent.interfaces import IObjectModifiedEvent >>> from zope.event import notify >>> from zope.app.component.interfaces import ISite >>> from zope.interface import directlyProvides, directlyProvidedBy >>> from zope.publisher.browser import TestRequest >>> from zope import event >>> from zope.app.folder import folder >>> myfolder = folder.rootFolder() This will make the commands rendered as test-friendly structures. >>> request = TestRequest() >>> directlyProvides(request, directlyProvidedBy(request) + IDebugRequest) Now we will write our custom. >>> class SampleView(AzaxBaseView): ... def add_page(self, title): ... # normally you would change the zope database here ... event.notify(ObjectModifiedEvent(title)) ... return self.render() >>> view = SampleView(myfolder, request) >>> view.add_page("some title") [] Now that we have shown that this will not generate any Azax commands we will register a handler. This handler will catch the object event and add some Azax commands. >>> @component.adapter(IAzaxEvent) ... def stuff_happend(azax_event): ... orig_event = azax_event.event ... azax_event.view.getCommandSet('core').replaceInnerHTML( ... 'div.class', orig_event.object) >>> component.provideHandler(stuff_happend) When we call the renderer now it should have some more data in it. >>> view = SampleView(myfolder, request) >>> view.add_page("some title")[0]['params']['html'] u'some title'