Package: bda.plone.ajax
Integration packageThis is the plone integration package forbdajax.InstallationMake egg available in your instance.Apply extension profile.UsageFor detailed documentation aboutbdajaxplease refer tobdajax.Implement ajax action as browser viewAn ajax action can be implemented as simple browser view. The easiest way is to
render a template only. Create templatetile_a.pt:<divxmlns:ajax="http://namesspaces.bluedynamics.eu/ajax"tal:omit-tag=""><!-- the tile --><divclass="tile_a"style="background-color:#ffc261;"><h3>IamtileA</h3><!-- perform action directly. here we render tile_b, see below --><ahref=""ajax:bind="click"ajax:action="bdajax_example_tile_b:.tile_a:replace"ajax:target=""tal:attributes="ajax:target context/absolute_url">alterme</a></div></div>Configure via ZCML:<browser:pagefor="*"name="bdajax_example_tile_a"template="tile_a.pt"permission="zope2.View"/>Implement ajax action as content providerCreate a templatetile_b.ptcontaining the markup:<divxmlns:ajax="http://namesspaces.bluedynamics.eu/ajax"tal:omit-tag=""><!-- bind custom event to tile, perform action if event triggered --><!-- when event gets triggered, tile_a gets rendered, see above --><divclass="tile_b"style="background-color:#61ff68;"ajax:bind="altertile"ajax:action="bdajax_example_tile_a:.tile_b:replace"><h3>IamtileB</h3><!-- bind element to click event and trigger custom event --><ahref=""ajax:bind="click"ajax:event="altertile:.tile_b"ajax:target=""tal:attributes="ajax:target context/absolute_url">alterme</a></div></div>Create content provider inprovider.py:fromAcquisitionimportExplicitfromzope.interfaceimport(Interface,implementer,)fromzope.componentimportadapterfromzope.publisher.interfaces.browserimport(IBrowserRequest,IBrowserView,)fromzope.contentprovider.interfacesimportIContentProviderfromProducts.Five.browser.pagetemplatefileimportViewPageTemplateFilefrombda.plone.ajaximportajax_message@implementer(IContentProvider)@adapter(Interface,IBrowserRequest,IBrowserView)classProvider(Explicit):template=ViewPageTemplateFile(u'tile_b.pt')def__init__(self,context,request,view):self.__parent__=viewself.context=contextself.request=requestdefupdate(self):passdefrender(self):# set here continuation message. See bda.plone.ajax.__init__ for# details.ajax_message(self.request,'Demo continuation message',flavor='info')returnself.template(self)Configure provider via ZCML:<adaptername="bdajax_example_tile_b"provides="zope.contentprovider.interfaces.IContentProvider"factory=".provider.Provider"/>Implement a wrapper viewThe two ajax action rendering snippets above each render a tile only. now we
need to wrap this inside a plone view. Create templateploneview.pt:<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"xmlns:tal="http://xml.zope.org/namespaces/tal"xmlns:metal="http://xml.zope.org/namespaces/metal"xmlns:i18n="http://xml.zope.org/namespaces/i18n"lang="en"metal:use-macro="here/main_template/macros/master"><body><metal:mainfill-slot="main"><tal:main-macrometal:define-macro="main"><tal:tilereplace="structure context/@@bdajax_example_tile_a"/></tal:main-macro></metal:main></body></html>And register via ZCML:<browser:pagefor="*"name="bdajax_example_view"template="ploneview.pt"permission="zope2.View"/>Now start instance and navigate to@@bdajax_example_view. You get initiallytile arendered switching totile bon click and vise versa. This code
equates the one contained in examples folder.Implement an ajax formCreate a view which renders a form.Create template namedajaxform.pt. The attributeajax:formtells
bdajax to handle this form:<formxmlns:ajax="http://namesspaces.bluedynamics.eu/ajax"id="example_ajaxform"method="post"enctype="multipart/form-data"ajax:form="True"tal:define="error view/error"tal:attributes="action view/form_action"><labelfor="field">Field</label><divtal:condition="error"tal:content="error"style="font-weight:bold;color:red;">ErrorText</div><inputtype="text"name="field"tal:attributes="value view/value"/><inputtype="submit"name="submit"value="Submit"/></form>Create the view class:fromProducts.FiveimportBrowserViewfrombda.plone.ajaximportajax_continuefrombda.plone.ajaximportajax_form_fiddlefrombda.plone.ajaximportAjaxMessageclassAjaxForm(BrowserView):@propertydefform_action(self):return'ajaxform?form_name=bdajax_example_form'@propertydefsubmitted(self):return'field'inself.request.form@propertydeferror(self):ifnotself.submitted:returnifnotself.request.form['field']:returnu'Field must not be empty'@propertydefvalue(self):returnself.request.form.get('field')def__call__(self):ifself.submittedandnotself.error:ajax_continue(self.request,AjaxMessage('Success!','info',None))Register view via ZCML:<browser:pagefor="*"name="bdajax_example_form"class=".AjaxForm"template="ajaxform.pt"permission="zope2.View"/>Create wrapper view for form namedajaxformview.pt:<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"xmlns:tal="http://xml.zope.org/namespaces/tal"xmlns:metal="http://xml.zope.org/namespaces/metal"xmlns:i18n="http://xml.zope.org/namespaces/i18n"lang="en"metal:use-macro="here/main_template/macros/master"><body><metal:mainfill-slot="main"><tal:main-macrometal:define-macro="main"><tal:tilereplace="structure context/@@bdajax_example_form"/></tal:main-macro></metal:main></body></html>And register via ZCML:<browser:pagefor="*"name="bdajax_example_form_view"template="ajaxformview.pt"permission="zope2.View"/>Now start instance and navigate to@@bdajax_example_form_view.Implement ajax batchCreate a batch implementation in python, i.e.examplebatch.pycalculating
batch vocab:fromProducts.FiveimportBrowserViewfrombda.plone.ajax.batchimportBatchRESULTLEN=45SLICESIZE=10classExampleBatch(Batch):batchname='examplebatch'@propertydefvocab(self):ret=list()# len resultcount=RESULTLEN# entries per pageslicesize=SLICESIZE# number of batch pagespages=count/slicesizeifcount%slicesize!=0:pages+=1# current batch pagecurrent=self.request.get('b_page','0')foriinrange(pages):# create query with page numberquery='b_page=%s'%str(i)# create batch target urlurl='%s?%s'%(self.context.absolute_url(),query)# append batch pageret.append({'page':'%i'%(i+1),'current':current==str(i),'visible':True,'url':url,})returnretCreate batched result view:classBatchedResult(BrowserView):@propertydefbatch(self):returnExampleBatch(self.context,self.request)()@propertydefslice(self):result=range(RESULTLEN)current=int(self.request.get('b_page','0'))start=current*SLICESIZEend=start+SLICESIZEreturnresult[start:end]Create batched result template, i.e.batchedresult.pt:<divxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"xmlns:tal="http://xml.zope.org/namespaces/tal"xmlns:i18n="http://xml.zope.org/namespaces/i18n"i18n:domain="bda.plone.ajax"class="examplebatchsensitiv"ajax:bind="batchclicked"tal:attributes="ajax:target context/absolute_url;
                     ajax:action string:bdajax_example_batched_result:.examplebatchsensitiv:replace"><tal:listingbatchreplace="structure view/batch"/><ul><lital:repeat="item view/slice"tal:content="item">x</li></ul><tal:listingbatchreplace="structure view/batch"/></div>Create wrapper view, i.e.batchview.pt:<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"xmlns:tal="http://xml.zope.org/namespaces/tal"xmlns:metal="http://xml.zope.org/namespaces/metal"xmlns:i18n="http://xml.zope.org/namespaces/i18n"lang="en"metal:use-macro="here/main_template/macros/master"i18n:domain="bda.plone.ajax"><body><metal:mainfill-slot="main"><tal:main-macrometal:define-macro="main"><tal:tilereplace="structure context/@@bdajax_example_batched_result"/></tal:main-macro></metal:main></body></html>And register views via ZCML:<browser:pagefor="*"name="bdajax_example_batch"template="batchview.pt"permission="zope2.View"/><browser:pagefor="*"name="bdajax_example_batched_result"class=".examplebatch.BatchedResult"template="batchedresult.pt"permission="zope2.View"/>Now start instance and navigate to@@bdajax_example_batch. You get an
example result rendered batched. This code equates the one contained in
examples folder.ExamplesThis package ships with examples, as explained above.
To enable examples includebda.plone.ajax.examplesvia ZCML.Source CodeIf you want to help with the development (improvement, update, bug-fixing, …) ofbda.plone.ajaxthis is a great idea!The code is located in thegithub collective.You can clone it orget access to the github-collectiveand work directly on the project.Maintainers are Robert Niederreiter and the BlueDynamics Alliance developer team.
We appreciate any contribution and if a release is needed to be done on pypi, please just contact one of us:dev@bluedynamics dot comContributorsRobert Niederreiter (Autor)Jens W. KleinChanges2.0 (2019-03-10)Drop Plone 4 support (Needs 5.1+).
[agitator]Add Python 3 support.
[agitator]Minimal custom jquerytools with overlay added, no more dependency onplone.app.jquerytools.
[agitator]1.8 (2019-02-08)Depend onProducts.CMFPloneinstead ofPloneto not fetch unnecessary dependencies.
[thet]Check whether child is null before searching for elements by tag name in
ajax form response.
[rnix]Adopt tobdajax1.10.
[rnix]1.7 (2017-03-10)Add_authenticatorhidden field when rendering ajax forms in order to
make CSRF protection work properly.
[rnix]Plone 5 Update, registered as legacy bundle
[agitator, rnix]1.6 (2015-06-25)giveajaxactionresponse explicitContent-Type:application/json[jensens, 2015-06-25]log formerly catched and hidden exceptions with severity error to error log.
[jensens, 2015-06-25]Disable diazo theming inajaxactionandajaxformbrowser views.
[rnix, 2014-12-10]AddAjaxPathcontinuation. Can be used as ofbdajax1.6.
[rnix, 2014-12-10]1.5Add ajaxform convenience browser page.
[rnix, 2014-02-04]1.4Cleanup docs.
[rnix, 2013-10-21]Do not load examples by default.
[rnix, 2013-10-21]Add abstract batch for buidling ajax batches.
[rnix, 2013-10-20]1.3Provide overlay configuration.
[rnix, 2012-08-06]Provide form continuation.
[rnix, 2012-08-06]1.2.2render viewlet in IPortalTop, so it pops up centered and not at the end of
the site.
[jensens, 2011-12-02]add z3c.autoinclude entry point.
[jensens, 2011-12-02]1.2.1displaybdajax.messagewith traceback ifajaxactionthrows uncaught
exception.
[rnix]1.2add ajax continuation support and continuation helper objects and functions.
[rnix]1.1add examples.
[rnix]add ajaxaction view.
[rnix]1.0make it work.
[rnix]LicenseCopyright (c) 2010-2019, BlueDynamics Alliance, Austria
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.Neither the name of the BlueDynamics Alliance nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY BlueDynamics AllianceAS ISAND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BlueDynamics Alliance BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.