[Grok-dev] spotlight on: megrok.traject

Martijn Faassen faassen at startifact.com
Mon Jan 25 13:15:52 EST 2010


Hi there,

Aroldo Souza-Leite wrote:
> am I wrong in the expectation that an object of a non-Grok class 
> 'SomeForeignModel' (see attachment) would be instantiated by 'factory' in 
> 'TrajectForSomeForeignModel'
> as a result of the request defined in 'pattern' and that this object (or 
> some view of it) would be shown in the response?

This sounds correct. The view for the object would be shown in the 
response (possibly the 'index' view).

> If this is what 'megrok.traject' does to a non-Grok model, then I got 
> stuck where think I should implement a view for the non-Grok class 
> 'SomeForeignModel'. How can I do this?

In the usual way:

class Index(grok.View):
     grok.context(SomeForeignModel)

     def render(self):
         return "Hello!"

There are two issues that are special here. You can solve both by 
inheriting SomeForeignModel from grok.Context (or 
grokcore.component.Context, which is the same). But sometimes you can't. 
I should add this to the megrok.traject documentation.

One is auto-associating with contexts. Normally Grok associates views 
and adapters and such with contexts if they're in the same module. With 
this you'd need to be explicit, using 'grok.context' (on the view, or on 
module-level).

The other one concerns default views. By default, the ZTK looks for a 
default view named "index.html", not "index". Grok overrides this for 
all grok.Context objects using a ZCML directive. SomeForeignModel 
however is *not* a grok.Context (unless you can depend on 
grokcore.component, you can then use grokcore.component.Context).

Otherwise, you need this ZCML to tell the ZTK that *your* 
SomeForeignModel also uses 'index':

   <browser:defaultView
       for="SomeForeignModel"
       name="index"
       />

(or alternatively of course you can use grok.name('index.html'), but I 
think that's less clear)

Regards,

Martijn



More information about the Grok-dev mailing list