I am writing an application using the Traits-Enaml library. Most of my application is written in Atom, with the exception of some classes that need to display Chaco plots. These classes inherit from HasTraits. Example:
def SomeClass(HasTraits):
some_attr = Any() # Attribute that changes on interaction with plot
plot = Plot() # Chaco plot
Each of these SomeClass instances has a Chaco Plot which the user can interact with. These are displayed in my UI using the TraitsView widget. If the user clicks on a point in a plot, this triggers a change in some trait of the Someclass instance.
My model (Atom based) holds SomeClass instances, and a list of SomeClass instances.
def Model(Atom):
ex = Typed(SomeClass) # SomeClass instance
l = ContainerList(Typed(SomeClass)) # List of SomeClass instances
Based on this, I have two questions:
1) How can I observe changes in someclass_instance from within my Atom model?
When the user interacts with any of the plots, I want to trigger a function call within my model. If SomeClass would inherit from Atom, I could use @observe to listen for changes, and do a function call every time the observed attribute changes:
@observe('ex.some_attr')
def change_occurred(self,change):
print "ex.some_attr changed"
self.do_stuff()
However, @observe requires the observed attribute to be an Atom member. The traits in SomeClass are obviously not Atom members, so how can I resolve this? Does Traits-Enaml provide any tools to listen for changes in HasTraits objects from within Atom objects?
Alternatively, is there a better way to solve this?
2) How would I go about observing changes in attributes of any of the elements in the list?
If observing as described in the question above is possible, how would I observe changes in any of the elements of the list? In Traits I would observe changes in any of the list items as follows:
def AnotherClass(HasTraits):
some_attr = List(Instance(SomeClass)) # List of SomeClass instances which have some trait that changes during runtime.
However, I couldn't figure out how to listen for changes in the traits of any particular element in the list. This is also true for observing with Atom.
Any suggestions are greatly appreciated!