Skip to content

Commit

Permalink
Resolve sub properties in a template model (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- authored and Legioth committed Jun 8, 2016
1 parent b455491 commit afdc4a4
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ protected MapProperty getModelProperty(StateNode node, Binding binding) {
NodeMap model = node.getMap(NodeFeatures.TEMPLATE_MODELMAP);
String key = binding.getValue();
assert key != null;
if (key.contains(".")) {
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 model.getProperty(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.vaadin.hummingbird.template;

import com.vaadin.hummingbird.StateNode;
import com.vaadin.hummingbird.nodefeature.ModelMap;
import com.vaadin.hummingbird.template.model.ModelPathResolver;

import elemental.json.JsonValue;

Expand Down Expand Up @@ -52,7 +52,9 @@ public ModelValueBindingProvider(String key) {

@Override
public Object getValue(StateNode node) {
return node.getFeature(ModelMap.class).getValue(key);
ModelPathResolver resolver = new ModelPathResolver(key);
return resolver.resolveModelMap(node)
.getValue(resolver.getPropertyName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.vaadin.hummingbird.template.TemplateNode;
import com.vaadin.hummingbird.template.TemplateNodeBuilder;
import com.vaadin.hummingbird.template.TextTemplateBuilder;
import com.vaadin.hummingbird.template.model.ModelPathResolver;
import com.vaadin.hummingbird.template.parser.TemplateParser;
import com.vaadin.hummingbird.template.parser.TemplateResolver;

Expand Down Expand Up @@ -962,4 +963,18 @@ public static Optional<StateNode> getOverrideNode(Element element) {
}
}

@Test
public void testElementSubProperty() {
String modelPath = "bean.name";
ElementTemplateBuilder builder = new ElementTemplateBuilder("div")
.setProperty("prop", new ModelValueBindingProvider(modelPath));

Element element = createElement(builder);
StateNode stateNode = element.getNode();
ModelPathResolver r = new ModelPathResolver(modelPath);
r.resolveModelMap(stateNode).setValue(r.getPropertyName(), "John");

Assert.assertEquals("John", element.getProperty("prop"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.hummingbird.uitest.ui.template;

import com.vaadin.annotations.EventHandler;
import com.vaadin.hummingbird.template.model.TemplateModel;
import com.vaadin.ui.Template;

public class FormView extends Template {

static final String ID_FIRST_NAME = "firstName";
static final String ID_LAST_NAME = "lastName";
static final String ID_AGE = "age";

public interface ReadonlyFormModel extends TemplateModel {
public void setPerson(Person person);

public Person getPerson();
}

@Override
protected ReadonlyFormModel getModel() {
return (ReadonlyFormModel) super.getModel();
}

public FormView() {
Person person = new Person("Hello", "World", 32);

getModel().setPerson(person);
}

@EventHandler
public void updateModel() {
Person p = getModel().getPerson();
p.setFirstName(p.getFirstName() + "!");
p.setLastName(p.getLastName() + "?");
p.setAge(p.getAge() + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.vaadin.hummingbird.uitest.ui.template;

public class Person {
private String firstName, lastName;
private int age;

public Person() {
// Needed only for TemplateModel but can't be hidden
}

public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div>
<!-- All fields read only for now as there is no two way databinding -->
<div>
<div>First name</div>
<input id="firstName" disabled [value]="person.firstName" />
</div>
<div>
<div>Last name</div>
<input id="lastName" disabled [value]="person.lastName" />
</div>
<div>
<div>Age</div>
<input id="age" disabled [value]="person.age" />
</div>
<button (click)="$server.updateModel()">Update model values</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.hummingbird.uitest.ui.template;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.hummingbird.testutil.PhantomJSTest;
import com.vaadin.testbench.By;

public class FormIT extends PhantomJSTest {

@Test
public void updateServerSide() {
open();
Assert.assertEquals("Hello", getValue(FormView.ID_FIRST_NAME));
Assert.assertEquals("World", getValue(FormView.ID_LAST_NAME));
Assert.assertEquals("32", getValue(FormView.ID_AGE));

findElement(By.tagName("button")).click();

Assert.assertEquals("Hello!", getValue(FormView.ID_FIRST_NAME));
Assert.assertEquals("World?", getValue(FormView.ID_LAST_NAME));
Assert.assertEquals("33", getValue(FormView.ID_AGE));
}

private String getValue(String inputId) {
return findElement(By.id(inputId)).getAttribute("value");
}
}

0 comments on commit afdc4a4

Please sign in to comment.