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));
}
}
Aggregations