close

Filter

loading table of contents...

Studio Developer Manual / Version 2010

Table Of Contents

5.3.6 Value Expressions

The interface com.coremedia.ui.data.ValueExpression describes objects that provide access to a possibly mutable value and that notify listeners when the value changes. They may also allow you to receive a value that can then become the next value of the expression. Value expressions may be as simple as defining a one-to-one wiring of a widget property to a model property, but they may encapsulate complex logic that accesses many objects to determine a result value. As an application developer, you can think of value expressions as an abstraction layer that hides that potential complexity from you, and use a common, simple interface when wiring up UI state to complex model state.

The Studio SDK offers the following primary implementations of the ValueExpression interface. You can use the factory methods from com.coremedia.ui.data.ValueExpressionFactory to create a ValueExpression programmatically from ActionScript.

  • PropertyPathExpression. This is meant to be used in simple scenarios, where you want to attach a simple bean property to a corresponding widget property. It starts from a bean and navigates through a path of property names to a value. Long paths can be separated with a dot. You can obtain this value expression flavor using ValueExpressionFactory#create(expression, bean).

  • FunctionValueExpression. Use this in scenarios where your UI state requires potentially complex calculations on the model, using multiple beans (remote or local). This value expression object wraps an ActionScript function computing the expression's value. When a listener is attached to the returned value expression, the current value of the expression is cached, and dependencies of the computation are tracked. As soon as a dependency is invalidated, the cached value is invalidated and eventually a change event is sent to all listeners (if the computed value has actually changed). You can use ValueExpressionFactory#createFromFunction(function, ...args) to create this flavor. See below for details on how to use FunctionValueExpressions.

In many cases, you can use the facilities provided by plugins (and thus use MXML to specify your value expression), without ever constructing a value expression programmatically. Nevertheless, value expressions are a vital part of the Studio SDK's data binding framework, so it is helpful to understand how they work.

Values

The method getValue() returns the current value of the expression. How this value is computed depends on the type of value expression used. Like bean properties, value expressions may evaluate to any ActionScript value.

When a value expression accesses remote beans that have not yet been loaded, its value is undefined. Getting the value or attaching a change listener (see below) subsequently triggers loading all remote bean necessary to evaluate the expression. If you need a defined value, you can use the loadValue(Function) method instead. The loadValue method ensures that all remote beans have been loaded and only then calls back the given function (and, in contrast to change listeners, only once, see below) with the concrete value, which is never undefined.

Like remote beans, value expressions may turn out to be unreadable due to missing read rights. In this case, getValue() returns undefined, too, and the special condition is signaled by the method isReadable() returning false.

Events

A listener may be attached to a value expression using the method addChangeListener(listener) and removed using the method removeChangeListener(listener). The listener must be a function that takes the value expression as its single argument. The listener may then query the value expression for the current value.

Contrary to bean events, value expression events are sent asynchronously after the calls modifying the value have already completed. The framework does however not guarantee that listeners are notified on all changes of the value. When the value is updated many times in quick succession, some intermediate values might not be visible to the listener.

The listener is also notified when the readability of the value changes.

As long as you have a listener attached to a value expression, the value expression may in turn be registered as a listener at other objects. To make sure that the value expression can be garbage collected, you must eventually remove all listeners added to it.

A common pattern when adding a listener to a value expression involves an upfront initialization and subsequent updates on events:

import com.coremedia.ui.data.ValueExpression;

public class MyComponentBase extends AnExtJSComponent {
  private var valueExpr:ValueExpression;

  public function MyComponentBase(config:MyComponentBase = null) {
    ...
    valueExpr:ValueExpression = ...;
    valueExpr.addChangeListener(valueExprChanged);
    valueExprChanged(valueExpr);
    ...
  }

  private function valueExprChanged(valueExpr:
    ValueExpression):void
  {
    var value:* = valueExpr.getValue();
    ...
  }

  override protected function onDestroy():void {
    valueExpr && valueExpr.removeChangeListener(valueExprChanged);
    ...
    super.onDestroy();
  }
  ...
}

Example 5.9. Adding a listener and initializing


By calling the private function once immediately after adding the listener, it is possible to reuse the functionality of the listener for initializing the component. By removing the listener on destroy, memory leaks due to spurious listeners are avoided.

Property Path Expressions

The most commonly used value expression is the property path expression. It allows you to navigate from an object to a value by successively reading property values on which the next read operation takes place. For example, a property path expression may operate on the object obj and be configured to read the properties a, b, and then c. If the property a of obj is obj1, the property b of obj1 is obj2, and the property c of obj2 is 4, then the expression will evaluate to 4. A path of property names is denoted by a string that joins the property names with dots, in this case "a.b.c". If you want to address array elements you have to add the index of the element with another dot, such as a.b.c.3, and not use the more obvious but false a.b.c[3] notation.

You can create a property path expression manually in the following way:

import com.coremedia.ui.data.ValueExpression;
import com.coremedia.ui.data.ValueExpressionFactory;
...
var ppe:ValueExpression =
  ValueExpressionFactory.create("a.b.c", obj);

Example 5.10. Creating a property path expression


The dot notation above might suggest that property path expressions operate exactly like ActionScript expressions, but that is not quite correct. Property path expressions support the following access methods for properties:

  • read the property of a bean using the get(property) method;

  • call a publicly defined getter method whose name consists of the string "get" followed the name of the property, first letter capitalized;

  • call a publicly defined getter method whose name consists of the string "is" followed the name of the property, first letter capitalized;

  • read from a publicly defined field of an object. This is the classic ActionScript case.

At different steps in the property path, different access methods may be used.

Even if there are many properties in the path, changes to any of the objects traversed while computing the value will trigger a recomputation of the expression value and potentially, if the value has changed, an event. This is only possible, however, for objects that can send property change events.

  • For beans, a listener is registered using addPropertyChangeListener().

  • For components using Ext.mixin.Observable, a listener is registered using addListener().

Property path expressions may be updated. When invoking setValue(value), a new value for the value expression is established. This will only work if the last property in the property path is writable for the object computed by the prefix of the path. More precisely, a value may be

  • written into a property of a bean using the set(property,value) method;

  • passed to a publicly defined setter method that takes the new value as its single argument and whose name consists of the string "set" followed by the name of the property, first letter capitalized;

  • written into a publicly defined field of an ActionScript class.

At various points of the API, a value expression is provided to allow a component to bind to varying data. Using the method extendBy(extensionPath) adds further property dereferencing steps to the existing expression. For example, ValueExpressionFactory.create("a.b.c", obj) is equivalent to ValueExpressionFactory.create("a", obj).extendBy("b.c").

To create a property path expression from within an MXML file, you can use the valueExpression element from the exml:com.coremedia.ui.config namespace.

...
<ui:ValueExpression expression="myProperty"
  context="{getModel()}"/>
...

Example 5.11. The valueExpression MXML element


Function Value Expressions

Function value expressions differ from property path expressions in that they allow arbitrary ActionScript code to be executed while computing their values. This flexibility comes at a cost, however: such an expression cannot be used to update variables, only to compute values. They are therefore most useful to compute complex GUI state that is displayed later on.

To create a function value expression, use the method createFromFunction of the class ValueExpressionFactory.

ValueExpressionFactory.createFromFunction(function():Object {
  return ...;
});

Example 5.12. Creating a function value expression


The function in the previous example did not take arguments. In this case, it can still use all variables in its scope as starting point for its computation or it might access global variables. To make the code more readable, you might want to define a named function in your ActionScript class and use that function when building the expression.

private function doSomething():void {
  ...
  expr = ValueExpressionFactory.
    createFromFunction(calculateSomething);
  ...
}

private function calculateSomething():Object {
  return ...;
}

Example 5.13. Creating a value expression from a private function


If you want to pass arguments to the function, you can provide them as additional argument of the factory method. The following code fragment uses this feature to pass a model bean to a static function.

private function doSomething():void {
  ...
  expr = ValueExpressionFactory.
    createFromFunction(calculateSomething, getModel());
  ...
}

private static function calculateSomething(model:Bean):Object
{
  return ...;
}

Example 5.14. Creating a value expression from a static function


Function value expressions fire value change events when their value changes. To this end, they track their dependencies on various objects when their value is computed. For accessed beans and value expressions, the dependency is taken into account automatically: whenever the bean or the value expression changes, the value of the function value expression changes automatically, and an event for the function value expression is fired.

If you access other mutable objects, you should make sure that these objects inherit from Observable, so that you can register the dependencies yourself. To this end, you can use the static methods of the class DependencyTracker. In particular, the method dependOnObservable(Observable,String) provides a way to specify the observable and the event name that indicates a relevant change. As a shortcut, the method dependOnFieldValue(Field) allows you to depend on the value of an input field.

var observable:Obserable;
var field:Field;

private function calculateSomething():Object {
  DependencyTracker.dependOnObservable(observable, "fooEvent");
  DependencyTracker.dependOnFieldValue(field);
  ... observable.fooMethod() ...;
  ... field.getValue() ...;
  return ...;
}

Example 5.15. Manual dependency tracking


If you register a dependency while no function value is being computed, the call to DependencyTracker is ignored. This means that you can register dependencies in your own functions, and the methods will work whether they are called in the context of a function value expression or not.

To create a function value expression in MXML, you have to insert an ActionScript block into the MXML:

...
<fx:Script><![CDATA[
  ...
  import com.coremedia.ui.data.ValueExpressionFactory;
  ...
]]></fx:Script>
...
<ui:BindPropertyPlugin bindTo="{ValueExpressionFactory.
  createFromFunction(calculateSomething)}"/>
...

Example 5.16. The BindPropertyPlugin MXML element


This assumes that you have defined a function calculateSomething in the Script block or in the base class of your MXML component with visibility protected.

The following listing contains a comprehensive example of a function value expression with detailed code comments concerning where and why dependency tracking is active or not. In the function, a list of titles is gathered from different sources. For each of the titles, a panel is searched and its height is put into a map. This map is the return value of the function.

private function listenToChanges():void {
  var firstContent:Content = getFirstContentValueExpression().getValue();
  var secondContentVE:ValueExpression = getSecondContentValueExpression();

  var panelHeightsVE:ValueExpression =
    ValueExpressionFactory.createFromFunction(
      getPanelHeights,
      firstContent,
      secondContentVE);
}

// First content is directly passed to the function.
// => No dependency tracking for changes to getFirstContentValueExpression().
// Second content is accessed via ValueExpression.
// => Dependency tracking for changes to getSecondContentValueExpression().
private function getPanelHeights(
  firstContent:Content,
  secondContentVE:ValueExpression):Object {

  // 'additionalTitles' is just a class field.
  // => No dependency tracking for changes to its value.
  var titles:Array = additionalTitles || [];

  // Accessing a Bean property.
  // => Dependency tracking for changes to the bean property.
  // Normal beans as opposed to the RemoteBeans below are not asynchronous,
  // so we do not need to wait until they are loaded.
  var model:Bean = getModel();
  titles = titles.concat(model.get('additionalTitles') || []);

  // Contents are of type Bean (RemoteBean).
  // RemoteBeanUtil.isAccessible() checks if loaded and readable.
  // If not:
  // (1) A 'load' call is automatically triggered.
  // (2) A dependency for a Bean state change is registered.
  // => dependency tracking for the content beans being loaded.
  switch (RemoteBeanUtil.isAccessible(firstContent)) {
    case undefined:
      // Not loaded yet.
      // => Interrupt computation. Wait for firstContent being loaded.
      return undefined;
    case true:
      // Loaded and unreadable.
      // => Abort
      return null;
    // Otherwise: RemoteBean loaded, just continue ...
  }

  // Dependency tracking for changes to secondContentVE.
  var secondContent:Content = secondContentVE.getValue();
  if (!secondContent) {
    // Interrupt computation.
    // Wait for secondContentVE holding a content.
    return undefined;
  }
  // See above: Wait for secondContent being loaded.
  switch (RemoteBeanUtil.isAccessible(secondContent)) {
    case undefined: return undefined;
    case true: return null;
  }

  // From here on, both contents are loaded
  // Their properties can be accessed.
  // Properties of contents are SubBeans => no need to wait
  // for them being loaded.
  var properties:ContentProperties = firstContent.getProperties();
  titles.push(properties.get("title"));
  properties = secondContent.getProperties();
  titles.push(properties.get("title"));

  var panelHeights:Object = {};

  // For all gathered titles, find a panel with the corresponding title
  // and get its height.

  var panelsParantContainer:Container = getPanelsParentContainer();

  var addDependencyAdded:Boolean = false;

  for (var i:int = 0; i < titles.length; i++) {
    var title:String = titles[i];
    var panel:Panel = panelsParantContainer.getPanelWithTitle(title);
    if (!panel) {
      // Panel with title does not exist yet.
      // Dependency tracking for new childs being added to the container.
      // 'add' is a component Event of Ext.container.Container
      if (!addDependencyAdded) {
        DependencyTracker.
          dependOnObservable(panelsParantContainer, "add");
          // Only add one dependency for 'add'.
          addDependencyAdded = true;
      }
      // Continue with next title.
      continue;
    }
    if (panel.rendered) {
      // If panel is rendered, just get its height.
      panelHeights[panel.getId()] = panel.getHeight();
    } else {
      // If panel is not rendered:
      // => Dependency tracking for the panel being rendered.
      // 'afterrender' is component event of Ext.Component
      DependencyTracker.dependOnObservable(panel, "afterrender");
    }
  }

  // Alternative:
  // According to the code above, also partial values for
  // 'panelHeights' are computed: Not found or not rendered
  // panels are just skipped. Alternatively, we could wait
  // until all panels are present and rendered. In that case
  // we need to return 'undefined' each time we encounter
  // a missing part. It really depends on what 'panelHeightsVE'
  // is supposed to deliver.

  return panelHeights;
}

Example 5.17. Comprehensive example of a FunctionValueExpression


Search Results

Table Of Contents