Search in sources :

Example 1 with BeanModelType

use of com.vaadin.client.flow.model.BeanModelType in project flow by vaadin.

the class AbstractTemplateStrategy method getModelBindingValue.

/**
 * Gets the value from the {@code node} for the {@code binding}.
 *
 * @param node
 *            the state node, not {@code null}
 * @param binding
 *            binding data, not {@code null}
 * @return map binding value, or an empty optional if no value for the
 *         binding
 */
private static Optional<Object> getModelBindingValue(StateNode node, Binding binding) {
    NodeMap model = node.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    String key = binding.getValue();
    assert key != null;
    if (!node.hasFeature(NodeFeatures.TEMPLATE)) {
        /*
             * This is temporary legacy logic to support *ngFor bindings. JS
             * evaluation should be used in any case. But at the moment JS
             * evaluation doesn't work with *ngFor bindings so they are handled
             * here.
             *
             * TODO: remove this and update JS evaluation to support *ngFor.
             */
        String[] modelPathParts = key.split("\\.");
        // The last part is the propertyName
        for (int i = 0; i < modelPathParts.length - 1; i++) {
            StateNode n = (StateNode) model.getProperty(modelPathParts[i]).getValue();
            model = n.getMap(NodeFeatures.TEMPLATE_MODELMAP);
        }
        key = modelPathParts[modelPathParts.length - 1];
        return Optional.ofNullable(model.getProperty(key).getValue());
    } else {
        String expression = key;
        String modelDescriptorId = (String) node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).getValue();
        assert modelDescriptorId != null;
        JsonObject modelDescriptor = node.getTree().getRegistry().getConstantPool().get(modelDescriptorId);
        NativeFunction function = new NativeFunction("model", "with(model) { return " + expression + "}");
        BeanModelType type = new BeanModelType(modelDescriptor);
        Object proxy = type.createProxy(model);
        return Optional.ofNullable(function.call(null, proxy));
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) JsonObject(elemental.json.JsonObject) BeanModelType(com.vaadin.client.flow.model.BeanModelType)

Aggregations

StateNode (com.vaadin.client.flow.StateNode)1 BeanModelType (com.vaadin.client.flow.model.BeanModelType)1 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)1 NativeFunction (com.vaadin.client.flow.util.NativeFunction)1 JsonObject (elemental.json.JsonObject)1