[PEAK] Using peak.model as an observer subject

Doug Quale quale1 at charter.net
Wed Sep 1 20:17:57 EDT 2004


"Phillip J. Eby" <pje at telecommunity.com> writes:

> At 11:08 AM 9/1/04 -0500, Doug Quale wrote:
> >I want to use peak.model as the model in MVC so I need notification of
> >updates.
> 
> Currently, you need to override the '_onLink'/'_onUnlink' methods of
> the features involved in order to do anything like this.
> 
> The long-term plan is that peak.storage will provide some kind of
> event support, but for right now this is the only way to do it.

Thanks for your instantaneous reply and your patience with my
elementary questions.  I had to take some time to try this out.

I have never used the method exporter facilities of peak.model but it
seems like they might be useful here.  Is this a workable approach?
Simple is OK because I don't need a very featureful observer pattern.


    class ObservableElement(model.Element):

        _callbacks = binding.Make(weakref.WeakValueDictionary)

        def _attach(self, feature, callback):
            """Attach the observer callback to the subject feature."""

        def _detach(self, feature, callback):
            """Attach the observer callback to the subject feature."""

        def _update(self, feature, item):
            """Notify all observers of the subject feature."""


    class ObservableAttribute(model.Attribute):

        newVerbs = [('attach', 'attach%(initCap)s')
                    ('detach', 'detach%(initCap)s')]

        def attach(feature, element, callback):
            element._attach(feature, callback)

        def detach(feature, element, callback):
            element._detach(feature, callback)

        def _onLink(feature, element, item, posn=None):
            element._update(feature, item)

        def _onUnlink(feature, element, item, posn=None):
            element._update(feature, item)



More information about the PEAK mailing list