Search in sources :

Example 1 with JsPropertyMap

use of jsinterop.base.JsPropertyMap in project vue-gwt by Axellience.

the class VueGwtExamplesService method addExample.

private static void addExample(String exampleId, VueFactory exampleVueFactory) {
    // If we find the containing div for this example, we instantiate it
    if (DomGlobal.document.getElementById(exampleId) != null) {
        VueComponent exampleInstance = Vue.attach("#" + exampleId, exampleVueFactory);
        ((JsPropertyMap) DomGlobal.window).set(exampleId, exampleInstance);
    }
}
Also used : VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) JsPropertyMap(jsinterop.base.JsPropertyMap)

Example 2 with JsPropertyMap

use of jsinterop.base.JsPropertyMap in project vue-gwt by Axellience.

the class VueGWTTools method wrapMethod.

/**
 * Proxy a method call to be warned when it called. This requires the
 * function to be JsInterop (name shouldn't change at runtime). This used to
 * observe Java Collections/Map. It won't be necessary in future versions of
 * Vue.js based on ES6 proxies.
 * @param object The object to observe
 * @param methodName The name of the method to proxify
 * @param afterMethodCall A callback called each time after the method has
 * been executed
 * @param <T> Type of the object the we Proxy
 */
public static <T> void wrapMethod(T object, String methodName, AfterMethodCall<T> afterMethodCall) {
    Function method = (Function) ((JsPropertyMap) object).get(methodName);
    WrappingFunction wrappingFunction = args -> {
        Object result = method.apply(object, args);
        afterMethodCall.execute(object, methodName, result, args);
        return result;
    };
    ((JsPropertyMap) object).set(methodName, wrappingFunction);
}
Also used : JsFunction(jsinterop.annotations.JsFunction) VueJsConstructor(com.axellience.vuegwt.core.client.vue.VueJsConstructor) Function(elemental2.core.Function) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) JsPropertyMap(jsinterop.base.JsPropertyMap) ComponentJavaPrototype(com.axellience.vuegwt.core.client.component.ComponentJavaPrototype) JsObject(elemental2.core.JsObject) JsFunction(jsinterop.annotations.JsFunction) Function(elemental2.core.Function) JsObject(elemental2.core.JsObject) JsPropertyMap(jsinterop.base.JsPropertyMap)

Example 3 with JsPropertyMap

use of jsinterop.base.JsPropertyMap in project vue-gwt by Axellience.

the class VueGWTTools method extendVueConstructorWithJavaPrototype.

/**
 * Copy a Java class prototype to a VueComponent declaration. This allows
 * VueComponent created by Vue to pass as an instance of the VueComponent
 * class they implement.
 * @param extendedVueJsConstructor The Vue.js constructor function to extend
 * @param componentJavaPrototype The VueComponent class JS prototype to
 * extend with
 * @param <T> The type of the VueComponent
 */
public static <T extends VueComponent> void extendVueConstructorWithJavaPrototype(VueJsConstructor<T> extendedVueJsConstructor, ComponentJavaPrototype<T> componentJavaPrototype) {
    JsPropertyMap vueProto = (JsPropertyMap) ((JsPropertyMap) extendedVueJsConstructor).get("prototype");
    JsObject vueProtoJsObject = ((JsObject) vueProto);
    componentJavaPrototype.forEach(protoProp -> {
        if (!vueProtoJsObject.hasOwnProperty(protoProp))
            vueProto.set(protoProp, componentJavaPrototype.get(protoProp));
    });
}
Also used : JsObject(elemental2.core.JsObject) JsPropertyMap(jsinterop.base.JsPropertyMap)

Example 4 with JsPropertyMap

use of jsinterop.base.JsPropertyMap in project vue-gwt by Axellience.

the class VueGWTObserverManager method initClassPropertiesCache.

private Map<String, Object> initClassPropertiesCache(JsObject object, String className) {
    final Map<String, Object> tmpCache = new HashMap<>();
    classesPropertyCaches.put(className, tmpCache);
    JsPropertyMap prototype = (JsPropertyMap) object.__proto__;
    prototype.forEach(property -> {
        Object value = prototype.get(property);
        if (isDefaultValue(value)) {
            tmpCache.put(property, value);
        }
    });
    return tmpCache;
}
Also used : HashMap(java.util.HashMap) JsObject(elemental2.core.JsObject) JsPropertyMap(jsinterop.base.JsPropertyMap)

Example 5 with JsPropertyMap

use of jsinterop.base.JsPropertyMap in project vue-gwt by Axellience.

the class VueGWTObserverManager method makeStaticallyInitializedPropertiesReactive.

/**
 * Due to GWT optimizations, properties on java object defined like this are not observable in
 * Vue.js when not running in dev mode:
 * <br>
 * private String myText = "Default text";
 * private int myInt = 0;
 * <br>
 * This is because GWT define the default value on the prototype and don't define it on the
 * object.
 * Therefore Vue.js don't see those properties when initializing it's observer.
 * To fix the issue, we manually look for those properties and set them explicitly on the
 * object.
 * @param object The Java object to observe
 * @param className The Java class name to observe
 */
private void makeStaticallyInitializedPropertiesReactive(JsObject object, String className) {
    Map<String, Object> cache = classesPropertyCaches.get(className);
    if (cache == null) {
        cache = initClassPropertiesCache(object, className);
    }
    JsPropertyMap javaObjectPropertyMap = ((JsPropertyMap) object);
    cache.forEach((key, value) -> {
        if (!object.hasOwnProperty(key))
            javaObjectPropertyMap.set(key, value);
    });
}
Also used : JsObject(elemental2.core.JsObject) JsPropertyMap(jsinterop.base.JsPropertyMap)

Aggregations

JsPropertyMap (jsinterop.base.JsPropertyMap)9 JsObject (elemental2.core.JsObject)5 JsOverlay (jsinterop.annotations.JsOverlay)3 VueComponent (com.axellience.vuegwt.core.client.component.VueComponent)2 ComponentJavaPrototype (com.axellience.vuegwt.core.client.component.ComponentJavaPrototype)1 PropOptions (com.axellience.vuegwt.core.client.component.options.props.PropOptions)1 VueJsConstructor (com.axellience.vuegwt.core.client.vue.VueJsConstructor)1 Function (elemental2.core.Function)1 HashMap (java.util.HashMap)1 JsFunction (jsinterop.annotations.JsFunction)1