close

Filter

loading table of contents...

Studio Developer Manual / Version 2304

Table Of Contents
Merging Config Objects

When receiving a Config object, the typical things a constructor does is:

  • Apply the received config on its own Config defaults

  • Hand through the resulting Config to its super constructor

In TypeScript code, this could be done like this:

constructor(config: Config<MyClass>) {
  super(Object.assign(Config<MyClass>({
    id: "4711",
    configOption1: "bar",
    configOption2: [42, 24]
  }), config));
}

Example 5.16.  Typical work of constructor done in TypeScript


However, there is a special utility class named ConfigUtils that helps implementing a specific merge logic. For array-valued properties, it should be possible to, instead of replacing the whole array, append or prepend to the existing value. The concrete use cases where this often makes sense are Ext component's plugins and items properties. So at least if your class has any array-valued properties, you should use the following in your constructor:

import ConfigUtils from "@jangaroo/runtime/ConfigUtils";

  //...

    constructor(config: Config<MyClass>) {
      super(ConfigUtils.apply(Config<MyClass>({
        id: "4711",
        configOption1: "bar",
        configOption2: [42, 24]
      }), config));
    }

  //...

Example 5.17.  Using ConfigUtils utility class


Any client using such a component can then use the following:

import ConfigUtils from "@jangaroo/runtime/ConfigUtils";

  //...

      Config(MyClass, {
        id: "4711",
        configOption1: "bar",
        ...ConfigUtils.append({
          configOption2: [12]
        }
      }), config));
    }

  //...

Example 5.18.  Component with utility class in client


The resulting value of configOption2 after merging via ConfigUtils.apply() will be [42, 24, 12]. There is an analogous utility method ConfigUtils.prepend(). Both return an object, handing through the given property, complementing it by an internal marker property that specifies where to insert the value into the previous value. To "lift" these properties into the surrounding object literal, the spread operator ... is used.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.