RE: [Zope-dev] ZClasses instances become detached from class
This distinction between class and instance is significant. One caveat concerns folders: Don't make the mistake of creating a folder within your class. If you do that, each instance will inherit this folder, as expected. But any modifications to perform on the folder -- including adding objects -- will show up in all instances. (Of course, there might be cases where this is exactly the intended effect.) So in this case you should not create the folder within the class, but let one of your methods create it.
(Imho, this is a weakness in the current ZClass model: You can't add constructors. So given the previous paragraph, there's no way you can say, "When an instance of this class is created, such and such folder should be created within the instance.")
Well, technically there is a constructor of sorts - the DTML method or External method that is the target of the "Add" form for your ZClass is basically the equivalent of a Python constructor, except that it is outside-in, rather than inside-out. Where in Python you might do: def __init__(self, id): self.id=id self.sub=self.CreateNewFolder('blah') ...in the target of your Zope Add form for the ZClass you would do something like this if you were using a DTML Method: <!-- Create an instance of our class in the target object --> <!--#with "MyZClass.createInObjectManager(REQUEST['id'], REQUEST)"--> <!-- Set properties based on the contents of the Add form --> <!--#call "propertysheets.Basic.manage_changeProperties( REQUEST)"--> <!-- Create a new Folder in the new MyZClass instance --> <!--#call "manage_addFolder('foobar')"--> <!--#/with--> ...or in an external method you might do: def addAMyZClass(self, id, REQUEST=None): """ """ # Get the actual destination object, using the this() # method, to be sure we get it in the right context.. self=self.this() # Create the new instance newob=self.MyZClass(id) newob.id=id # Set properties based on the contents of the Add form newob.propertysheets.Basic.manage_changeProperties(REQUEST) # Add a new Folder to the new instance newob.manage_addFolder('foobar') # Add the new instance to the target object self._setObject(id, newob) return 'OK!' Hope this helps! Brian Lloyd brian@digicool.com Software Engineer 540.371.6909 Digital Creations http://www.digicool.com
Zopists, I noticed a post in the list a while back about Subclassing. In order to make a ZClass inherit from a Base ZClass like lets say a "Findable" :) base class. Then I have to put all of the ZClasses that I want to decend from Findable inside the Findable ZClass. Since they don't show up in the "Add ZClass" interface. Won't this means that the Findable ZClass will be very fat indeed? I'm probably misinterpreting the Subclass issue. All my best, Jason Spisak webmaster@mtear.com
participants (2)
-
Brian Lloyd -
Jason Spisak