Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: moving "subscription" expressions from the view into the model #67

Closed
jason-s opened this issue Oct 14, 2013 · 1 comment

Comments

@jason-s
Copy link

jason-s commented Oct 14, 2013

The << operator is great for putting expressions into the view. For instance if the model contains a property that is a floating point number, the view can decide how to format it and will automatically be updated.

But I would like to have the auto-update feature of subscription expressions in the model as well; there are derived calculations which depend on the raw model properties that should be managed by the model, not by the view.

Is there a way to do this?

example class:

class ItemWithQuantityDiscount(Atom):
  quantity = Int()
  basePrice = Float()
  def getQuantityDiscount(self):
      # some complicated formula goes here, depends on quantity
  def getUnitPrice(self):
     return self.basePrice * self.getQuantityDiscount()

and in the view I would like to have:

 Label:
    text << '${0:2f} each'.format(model.getUnitPrice())

From http://docs.enthought.com/enaml/instructional/tut_hello_world.html (yeah, i know, that's the old enaml, but I couldn't find the new docs)

Subscription. RHS can be any expression. The expression will be parsed for dependencies, and any dependency which is a trait attribute on a HasTraits class will have a listener attached. When the listener fires, the expression will be re-evaluated and the value of the view property will be updated.

@sccolbert
Copy link
Member

What you want is something like a dependent property. Traits has this in the form of the Property trait and cached properties. Atom also has something similar, though the dependency tracking is not provided for you. As it stands, I'm not happy with any of the existing dependent property behaviors and regret adding one to Atom. I have seen the pattern abused far too often where it leads to slow, unmaintainable code.

That said, there is value in a pattern like that, I just don't think dependent properties is the right way to go about it. I'll need to spend some time thinking about the right way to expose behavior like this, so it scales in an efficient manner.

In any case, this is an issue for Atom, not Enaml, so I will close this topic and ask that you open a new issue on the Atom tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants