Skip to content

Commit

Permalink
Support lazy init of vue properties
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 30, 2021
1 parent bfcb648 commit f93a577
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
22 changes: 20 additions & 2 deletions vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,9 +80,16 @@ public String getValueAsJson() throws JsonProcessingException {
return VueSettings.get().getObjectMapper().writeValueAsString(getValue());
}

public String getInitialValueAsJson() throws JsonProcessingException {
IModel<T> 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(),
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -27,12 +30,17 @@ public class DataFiberBuilder<M, V extends IVueBehaviorLocator> {
}

public DataFiberBuilder<M, V> bindToProperty() {
return bindToProperty(null);
return bindToProperty((IModel<M>)null);
}

public DataFiberBuilder<M, V> bindToProperty(M initPropValue) {
return bindToProperty((IModel<M>)(initPropValue==null?null:Model.of((Serializable)initPropValue)));
}

public DataFiberBuilder<M, V> bindToProperty(IModel<M> initPropValue) {
this.type = DataFiberType.PROPERTY;
this.initPropValue = initPropValue;
this.init = this.init || initPropValue!=null;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class DataFibersGroup implements Iterable<DataFiber<?>>, IDetachable {

private static final long serialVersionUID = 1L;

public static final SerializablePredicate<DataFiber<?>> PROPERTY_DATAFIBERS = df -> DataFiberType.PROPERTY.equals(df.getType());
public static final SerializablePredicate<DataFiber<?>> INIT_DATAFIBERS = DataFiber::shouldInit;
// public static final SerializablePredicate<DataFiber<?>> PROPERTY_DATAFIBERS = df -> DataFiberType.PROPERTY.equals(df.getType());
// public static final SerializablePredicate<DataFiber<?>> INIT_DATAFIBERS = DataFiber::shouldInit;
public static final SerializablePredicate<DataFiber<?>> INIT_BY_CLIENT_DATAFIBERS = DataFiber::shouldInitByClient;
public static final SerializablePredicate<DataFiber<?>> INIT_PROPS_DATAFIBERS = DataFiber::shouldInitPropAttribute;
public static final SerializablePredicate<DataFiber<?>> UPDATE_DATAFIBERS = DataFiber::shouldUpdate;
public static final SerializablePredicate<DataFiber<?>> OBSERVE_DATAFIBERS = DataFiber::shouldObserve;

Expand Down
6 changes: 3 additions & 3 deletions vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Collection<String> getWatch() {
}

public Collection<String> getInit() {
return dataFibers.getDataFibersNames(DataFibersGroup.INIT_DATAFIBERS);
return dataFibers.getDataFibersNames(DataFibersGroup.INIT_BY_CLIENT_DATAFIBERS);
}

public Collection<String> getObserve() {
Expand Down Expand Up @@ -229,7 +229,7 @@ protected void onComponentTag(ComponentTag tag) {
try {
String config = VueSettings.get().getObjectMapper().writeValueAsString(configView);
tag.put("vc-config", config );
List<DataFiber<?>> propertyDataFibers = dataFibers.getDataFibers(DataFibersGroup.PROPERTY_DATAFIBERS);
List<DataFiber<?>> propertyDataFibers = dataFibers.getDataFibers(DataFibersGroup.INIT_PROPS_DATAFIBERS);
if(!propertyDataFibers.isEmpty()) {
Set<String> normilizedAttrs = tag.getAttributes().keySet();
if(!normilizedAttrs.isEmpty())
Expand Down Expand Up @@ -310,7 +310,7 @@ protected void onBind() {

@VueMethod
public void vcInit(IVuecketMethod.Context ctx, Collection<String> toBeLoaded) {
Map<String, DataFiber<?>> initFibers = dataFibers.getDataFibersAsMap(DataFibersGroup.INIT_DATAFIBERS);
Map<String, DataFiber<?>> initFibers = dataFibers.getDataFibersAsMap(DataFibersGroup.INIT_BY_CLIENT_DATAFIBERS);
if(toBeLoaded!=null && !toBeLoaded.isEmpty()) initFibers.keySet().retainAll(toBeLoaded);
Map<String, Object> dataPatch = new HashMap<String, Object>();
Map<String, Object> propsPatch = new HashMap<String, Object>();
Expand Down
4 changes: 2 additions & 2 deletions vuecket/src/main/resources/org/orienteer/vuecket/vuecket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class VueCustomComponent extends VueComponent<String> {

public VueCustomComponent(String id, IModel<String> model) {
super(id, model);
dataFiberBuilder("content").bindToProperty().update().bind();
dataFiberBuilder("content").bindToProperty("INIT VALUE").update().bind();
}


Expand Down

0 comments on commit f93a577

Please sign in to comment.