diff --git a/vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java b/vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java index a82d7c6..cbba80b 100644 --- a/vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java +++ b/vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java @@ -6,6 +6,7 @@ import org.apache.wicket.model.IDetachable; import org.apache.wicket.model.IModel; import org.apache.wicket.model.IObjectClassAwareModel; +import org.apache.wicket.model.Model; import org.apache.wicket.util.io.IClusterable; import com.fasterxml.jackson.core.JsonProcessingException; @@ -79,9 +80,16 @@ public String getValueAsJson() throws JsonProcessingException { return VueSettings.get().getObjectMapper().writeValueAsString(getValue()); } + public String getInitialValueAsJson() throws JsonProcessingException { + IModel initModel = getInitPropModel(); + if(initModel!=null) { + return VueSettings.get().getObjectMapper().writeValueAsString(initModel.getObject()); + } else return getValueAsJson(); + } + public String getValueAsVueProperty(String componentId) throws JsonProcessingException { - String attrValue = getValueAsJson(); - if(shouldUpdate()) + String attrValue = getInitialValueAsJson(); + if(shouldUpdate() || getInitPropModel()!=null) attrValue = String.format("$vcDataFiber('%s', '%s', %s)", componentId, getName(), @@ -130,6 +138,16 @@ public boolean shouldInit() { return init; } + public boolean shouldInitByClient() { + return shouldInit() + && (DataFiberType.DATA.equals(getType()) + || (getInitPropModel()!=null && !Objects.equals(getModel(), getInitPropModel()))); + } + + public boolean shouldInitPropAttribute() { + return DataFiberType.PROPERTY.equals(getType()) && shouldInit(); + } + public boolean shouldUpdate() { return update; } diff --git a/vuecket/src/main/java/org/orienteer/vuecket/DataFiberBuilder.java b/vuecket/src/main/java/org/orienteer/vuecket/DataFiberBuilder.java index 1919b1c..b9f6cc9 100644 --- a/vuecket/src/main/java/org/orienteer/vuecket/DataFiberBuilder.java +++ b/vuecket/src/main/java/org/orienteer/vuecket/DataFiberBuilder.java @@ -1,6 +1,9 @@ package org.orienteer.vuecket; +import java.io.Serializable; + import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; import org.orienteer.vuecket.DataFiber.DataFiberType; /** @@ -27,12 +30,17 @@ public class DataFiberBuilder { } public DataFiberBuilder bindToProperty() { - return bindToProperty(null); + return bindToProperty((IModel)null); + } + + public DataFiberBuilder bindToProperty(M initPropValue) { + return bindToProperty((IModel)(initPropValue==null?null:Model.of((Serializable)initPropValue))); } public DataFiberBuilder bindToProperty(IModel initPropValue) { this.type = DataFiberType.PROPERTY; this.initPropValue = initPropValue; + this.init = this.init || initPropValue!=null; return this; } diff --git a/vuecket/src/main/java/org/orienteer/vuecket/DataFibersGroup.java b/vuecket/src/main/java/org/orienteer/vuecket/DataFibersGroup.java index 346cefe..35dfb8a 100644 --- a/vuecket/src/main/java/org/orienteer/vuecket/DataFibersGroup.java +++ b/vuecket/src/main/java/org/orienteer/vuecket/DataFibersGroup.java @@ -24,8 +24,10 @@ public class DataFibersGroup implements Iterable>, IDetachable { private static final long serialVersionUID = 1L; - public static final SerializablePredicate> PROPERTY_DATAFIBERS = df -> DataFiberType.PROPERTY.equals(df.getType()); - public static final SerializablePredicate> INIT_DATAFIBERS = DataFiber::shouldInit; +// public static final SerializablePredicate> PROPERTY_DATAFIBERS = df -> DataFiberType.PROPERTY.equals(df.getType()); +// public static final SerializablePredicate> INIT_DATAFIBERS = DataFiber::shouldInit; + public static final SerializablePredicate> INIT_BY_CLIENT_DATAFIBERS = DataFiber::shouldInitByClient; + public static final SerializablePredicate> INIT_PROPS_DATAFIBERS = DataFiber::shouldInitPropAttribute; public static final SerializablePredicate> UPDATE_DATAFIBERS = DataFiber::shouldUpdate; public static final SerializablePredicate> OBSERVE_DATAFIBERS = DataFiber::shouldObserve; diff --git a/vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java b/vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java index 0698fe1..b9a6a5f 100644 --- a/vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java +++ b/vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java @@ -85,7 +85,7 @@ public Collection getWatch() { } public Collection getInit() { - return dataFibers.getDataFibersNames(DataFibersGroup.INIT_DATAFIBERS); + return dataFibers.getDataFibersNames(DataFibersGroup.INIT_BY_CLIENT_DATAFIBERS); } public Collection getObserve() { @@ -229,7 +229,7 @@ protected void onComponentTag(ComponentTag tag) { try { String config = VueSettings.get().getObjectMapper().writeValueAsString(configView); tag.put("vc-config", config ); - List> propertyDataFibers = dataFibers.getDataFibers(DataFibersGroup.PROPERTY_DATAFIBERS); + List> propertyDataFibers = dataFibers.getDataFibers(DataFibersGroup.INIT_PROPS_DATAFIBERS); if(!propertyDataFibers.isEmpty()) { Set normilizedAttrs = tag.getAttributes().keySet(); if(!normilizedAttrs.isEmpty()) @@ -310,7 +310,7 @@ protected void onBind() { @VueMethod public void vcInit(IVuecketMethod.Context ctx, Collection toBeLoaded) { - Map> initFibers = dataFibers.getDataFibersAsMap(DataFibersGroup.INIT_DATAFIBERS); + Map> initFibers = dataFibers.getDataFibersAsMap(DataFibersGroup.INIT_BY_CLIENT_DATAFIBERS); if(toBeLoaded!=null && !toBeLoaded.isEmpty()) initFibers.keySet().retainAll(toBeLoaded); Map dataPatch = new HashMap(); Map propsPatch = new HashMap(); diff --git a/vuecket/src/main/resources/org/orienteer/vuecket/vuecket.js b/vuecket/src/main/resources/org/orienteer/vuecket/vuecket.js index 7a970ff..29ab41b 100644 --- a/vuecket/src/main/resources/org/orienteer/vuecket/vuecket.js +++ b/vuecket/src/main/resources/org/orienteer/vuecket/vuecket.js @@ -95,8 +95,8 @@ const Vuecket = { registerMethods.call(this, this.vcConfig.on, this.$on); registerMethods.call(this, this.vcConfig.once, this.$once); registerMethods.call(this, this.vcConfig.watch, this.$watch); - if(this.vcConfig.load) { - this.vcLoad(); + if(this.vcConfig.init) { + this.vcInit(); } if(this.vcConfig.observe) { this.vcConfig.observe.forEach(m => this.$watch(m, function(newValue){ diff --git a/vuecket/src/test/java/org/orienteer/vuecket/VueCustomComponent.java b/vuecket/src/test/java/org/orienteer/vuecket/VueCustomComponent.java index ff2b9f1..25b56e1 100644 --- a/vuecket/src/test/java/org/orienteer/vuecket/VueCustomComponent.java +++ b/vuecket/src/test/java/org/orienteer/vuecket/VueCustomComponent.java @@ -9,7 +9,7 @@ public class VueCustomComponent extends VueComponent { public VueCustomComponent(String id, IModel model) { super(id, model); - dataFiberBuilder("content").bindToProperty().update().bind(); + dataFiberBuilder("content").bindToProperty("INIT VALUE").update().bind(); }