Personal tools
You are here: Home Documentation How-Tos Using KSS actions on a view

Using KSS actions on a view

This How-to applies to: 0.12
This How-to is intended for: Developer

In many cases, KSS actions are using methods on a view. In Zope 3, traversing to the view and calling that method is one approach. In Grok, we can set the context to another view instead of a model object, which allows you to reuse the methods of the view through `self.view`.

Attention!

This howto works only with `megrok.kss 0.2dev`.

For this example add a method to your Index view class like this:

class Index(grok.View):

    def some_data(self):
        return '<p>Display some text in the view </p>'

In the template index.pt you can access this method:

<p tal:content="structure view/some_data">some data from a view</p>

In your app.py add the following lines to the top of your KSS action class:

from megrok.kss import KSSActionsForView

...

class AppKSS(KSSActionsForView):
    grok.view(Index)

    def reuse(self):
        extra_data = self.view.some_data() + '<p>from KSS</p>'
        core = self.getCommandSet('core')
        core.replaceInnerHTML('#message', extra_data)

The first line sets self.view to the Index view class. The method calls the method some_data on self.view. You can still access the content object through self.context.

Now that you have the KSS action in place you can add the KSS rule in static/app.kss:

#reuse-view:click {
    action-server: reuse url('@@index/@@reuse');
}

KSS tries to be standard about its use of relative urls. This implies that KSS chops the last piece of the url if it is not followed by a slash.

In Zope, the way to traverse to a view on a view is to explicitely access it through the @@ namespace. @@index/@@reuse means: "Please traverse to the reuse view on the index view of my context".

After restarting zope and refreshing the page the following steps are executed:

  • Clicking newly added div triggers the reuse method in the KSS view.
  • It calls the method on the view and adds an extra paragraph.
  • The contents div with the id message is replaced with the value in extra_data.