Plone 使用灵巧行为提供一种方法

Plone 使用灵巧行为提供一种方法,plone,zope,dexterity,grok,Plone,Zope,Dexterity,Grok,我一直在使用一个模式行为,没有问题,但我也想有一个提供一些逻辑的方法。现在我有 class IMyFoo(form.Schema): requester = schema.TextLine(title=_(u"Requestor"), required=False, ) def foo(self): """ foo """ alsoProvides(IMyFoo, IFormFieldProvider) 在zcml

我一直在使用一个模式行为,没有问题,但我也想有一个提供一些逻辑的方法。现在我有

class IMyFoo(form.Schema):
    requester = schema.TextLine(title=_(u"Requestor"),
              required=False,
          )

    def foo(self):
      """ foo """

alsoProvides(IMyFoo, IFormFieldProvider)
在zcml中

<plone:behavior
    title="My behavior"
    description="Some desc"
    provides=".behaviors.IMyFoo"
    for=".interfaces.ISomeInterface"
    />
<plone:behavior
    title="My behavior"
    description="Some desc"
    provides=".behaviors.IMyFoo"
    factory=".behaviors.MyFoo"
    for=".interfaces.ISomeInterface"
    />
在zcml中

<plone:behavior
    title="My behavior"
    description="Some desc"
    provides=".behaviors.IMyFoo"
    for=".interfaces.ISomeInterface"
    />
<plone:behavior
    title="My behavior"
    description="Some desc"
    provides=".behaviors.IMyFoo"
    factory=".behaviors.MyFoo"
    for=".interfaces.ISomeInterface"
    />

当然,像那样通过FTI不是正确的方法。但我现在不知所措。在Archetypes中,我只需要创建一个mixin类,并用我想要使用的任何内容类型继承它。我在这里也可以这样做,但我的理解是行为应该是它们的替代品,所以我想知道如何使用这个首选方法。

正如您所发现的,schema类实际上只是一个接口。它不能提供任何方法。为了提供更多的功能,您需要将行为接口连接到一个工厂类,该工厂类将调整敏捷对象以提供接口

因此,如果您的behaviors.py如下所示:

# your imports plus:
from plone.dexterity.interfaces import IDexterityContent
from zope.component import adapts
from zope.interface import implements

class IMyFoo(form.Schema):
    requester = schema.TextLine(
      title=_(u"Requestor"),
      required=False,
      )

    def foo(self):
      """ foo """

alsoProvides(IMyFoo, IFormFieldProvider)

class MyFoo(object):    
    implements(IMyFoo)
    adapts(IDexterityContent)

    def __init__(self, context):
        self.context = context

    def foo(self):
      return 'bar'
那么您唯一的zcml声明将是:

<plone:behavior
    title="My behavior name"
    description="Behavior description"
    provides=".behavior.IMyFoo"
    factory=".behavior.MyFoo"
    for="plone.dexterity.interfaces.IDexterityContent"
    />

注意IDexterityContent的使用。您正在创建可以应用于任何灵巧内容的行为。因此,对于非常通用的界面,行为适配器应该是

也许我应该只使用浏览器视图?我真的不需要对请求对象做任何事情,所以这看起来有点傻,但它会工作的。谢谢Steve。这仍然需要我知道我想要使用IMyFoo接口。如果我想找到所有提供foo方法的行为,我必须遍历fti中定义的行为列表?如果某个行为有一个foo方法做了你不想做的事情呢?组件体系结构的全部要点是隔离对象的各个方面。Python Zen的最后一个租户:“名称空间是一个非常好的主意——让我们做更多的事情吧!”听起来你只是在寻找一个或多个适配器。您可以为“*”定义适配器,然后为更具体的接口重写它。
<plone:behavior
    title="My behavior name"
    description="Behavior description"
    provides=".behavior.IMyFoo"
    factory=".behavior.MyFoo"
    for="plone.dexterity.interfaces.IDexterityContent"
    />
IMyFoo(myFooishObject).foo()