ConstraintJS - v0.10.2
    Preparing search index...

    Class Constraint<T>

    Note: the preferred way to create a constraint is with cjs(value) or cjs.constraint(value).

    A constraint communicates with the constraint solver to store and maintain a value. Its value can be a plain value, a function that computes it (whose dependencies are tracked automatically), or another constraint to follow.

    var x = cjs(1),
    y = cjs(function() { return x.get() + 1; });
    y.get(); // 2
    x.set(10);
    y.get(); // 11

    Type Parameters

    • T = any
    Index
    • this + args[0] + args[1] + .... Also concatenates strings, which is handy for units.

      Parameters

      • ...args: unknown[]

      Returns Constraint

      x = y.add(1, 2, z); // x <- y + 1 + 2 + z
      x = y.add("px"); // x <- y + "px"
    • false if this or any of args is falsy; otherwise the last value. Evaluation stops at the first falsy value (so, for example, cjs(false).and(a) never reads a).

      Parameters

      • ...args: unknown[]

      Returns Constraint

      var x = c1.and(c2, c3, true);
      
    • Removes every dependency and change listener, so that this constraint can be garbage collected.

      Parameters

      • Optionalsilent: boolean

        If true, don't invalidate the constraints that depended on this one

      Returns this

      remove

      var x = cjs(1);
      x.destroy(); // ...x is no longer needed
    • this / args[0] / args[1] / ...

      Parameters

      • ...args: unknown[]

      Returns Constraint<number>

      x = y.div(1, 2, z); // x <- y / 1 / 2 / z
      
    • Get the current value of this constraint, recomputing it first if it is invalid.

      Parameters

      • OptionalautoAddOutgoing: boolean

        Whether a constraint that is computing its value right now should start depending on this one (default: true)

      • OptionalgetterArg: unknown

        Passed as the second argument to this constraint's getter function, if it recomputes

      Returns T

      set

      var x = cjs(1);
      x.get(); // 1
    • Inline if, like this ? trueValue : otherValue.

      Parameters

      • trueValue: unknown
      • otherValue: unknown

      Returns Constraint

      var x = is_selected.iif(selected_val, nonselected_val);
      
    • Change this constraint's value depending on the state of an FSM.

      Parameters

      • fsm: FSM
      • values: Record<string, ConstraintSource<T>>

        For each state name, the value this constraint should have in that state

      Returns this

      var fsm = cjs.fsm("state1", "state2")
      .addTransition("state1", "state2", cjs.on("click"));
      var x = cjs().inFSM(fsm, {
      state1: 'val1',
      state2: function() { return 'val2'; }
      });
    • this instanceof other

      Parameters

      • other: unknown

      Returns Constraint<boolean>

      var valIsArray = val.instanceOf(Array);
      
    • Mark this constraint's value as invalid, so that it is recomputed the next time it's read. Anything that depends on it is invalidated too.

      Returns this

      isValid

      Tracking the window height
      var height = cjs(function() { return window.innerHeight; });
      window.addEventListener("resize", function() {
      height.invalidate();
      });
    • Whether this constraint's cached value is up to date. An invalid value is only recomputed when it's next read (for example, with .get()).

      Returns boolean

      invalidate

      var x = cjs(1),
      y = x.add(2);
      y.get(); // 3
      y.isValid(); // true
      x.set(2);
      y.isValid(); // false
      y.get(); // 4
      y.isValid(); // true
    • The natural logarithm, Math.log(this)

      Returns Constraint<number>

      num_digits = num.max(2).log().div(Math.log(10)).ceil();
      
    • The largest of this and args: Math.max(this, ...args)

      Parameters

      • ...args: unknown[]

      Returns Constraint<number>

      val = val1.max(val2, val3);
      
    • The smallest of this and args: Math.min(this, ...args)

      Parameters

      • ...args: unknown[]

      Returns Constraint<number>

      val = val1.min(val2, val3);
      
    • this * args[0] * args[1] * ...

      Parameters

      • ...args: unknown[]

      Returns Constraint<number>

      x = y.mul(1, 2, z); // x <- y * 1 * 2 * z
      
    • Removes the most recently added listener for callback. If thisArg is given, only a listener that was added with that thisArg is removed.

      Parameters

      • callback: (...args: any[]) => unknown
      • OptionalthisArg: unknown

      Returns this

      onChange

      var x = cjs(1),
      callback = function() {};
      x.onChange(callback);
      // ...
      x.offChange(callback);
    • Call callback when this constraint's value is invalidated. If it is invalidated several times before listeners run (for example, in a cjs.wait() batch), callback is only called once.

      Parameters

      • callback: (...args: any[]) => unknown
      • OptionalthisArg: unknown

        The this for callback (default: the global object)

      • ...args: unknown[]

        Arguments to pass to callback

      Returns this

      offChange

      var x = cjs(1);
      x.onChange(function() {
      console.log("x is " + x.get());
      });
      x.set(2); // x is 2
    • Like onChange, but listeners with a higher priority are called before those with a lower one (or none).

      Parameters

      • priority: number | false
      • callback: (...args: any[]) => unknown
      • OptionalthisArg: unknown
      • ...args: unknown[]

      Returns this

    • The first truthy value out of this and args, or false if none are. Evaluation stops at the first truthy value (so, for example, cjs(true).or(b) never reads b).

      Parameters

      • ...args: unknown[]

      Returns Constraint

      var x = c1.or(c2, c3, false);
      
    • Signal that this constraint's value will be computed later (for example, asynchronously). Until resumeGetter is called, get() returns temporaryValue. Call this from inside the constraint's getter function.

      Parameters

      • OptionaltemporaryValue: unknown

      Returns this

      resumeGetter

      var data = cjs(function(node) {
      node.pauseGetter("loading...");
      fetchData(function(result) {
      node.resumeGetter(result);
      });
      });
    • Math.pow(this, exponent)

      Parameters

      • exponent: unknown

      Returns Constraint<number>

      d = dx.pow(2).add(dy.pow(2)).sqrt();
      
    • A property of this constraint's value, like this[names[0]][names[1]]....

      Parameters

      • ...names: unknown[]

      Returns Constraint

      w = x.prop("y", "z"); // w <- x.y.z
      
    • Removes every dependency to and from this constraint.

      Parameters

      • Optionalsilent: boolean

        If true, don't invalidate the constraints that depended on this one

      Returns this

      destroy

    • Signal that this constraint, which was paused with pauseGetter, now has a value.

      Parameters

      • Optionalvalue: unknown

      Returns this

      pauseGetter

    • Change the value of this constraint. Anything that depends on it is invalidated.

      Parameters

      • value: ConstraintSource<T>

        The new value, a function to compute it, or a constraint to follow

      • Optionaloptions: { silent?: boolean }

        silent: true updates the value without invalidating anything that depends on it

      Returns this

      • get
      • invalidate
      var x = cjs(1);
      x.get(); // 1
      x.set(function() { return 2; });
      x.get(); // 2
      x.set('c');
      x.get(); // 'c'
    • Change how this constraint's value is computed (see ConstraintOptions).

      Parameters

      Returns this

      var x = cjs(function() { return 1; });
      x.get(); // 1
      x.setOption({ literal: true, auto_add_outgoing_dependencies: false });
      x.get(); // (function)

      x.setOption("literal", false);
      x.get(); // 1
    • Change how this constraint's value is computed (see ConstraintOptions).

      Type Parameters

      Parameters

      Returns this

      var x = cjs(function() { return 1; });
      x.get(); // 1
      x.setOption({ literal: true, auto_add_outgoing_dependencies: false });
      x.get(); // (function)

      x.setOption("literal", false);
      x.get(); // 1
    • this - args[0] - args[1] - ...

      Parameters

      • ...args: unknown[]

      Returns Constraint<number>

      x = y.sub(1, 2, z); // x <- y - 1 - 2 - z
      
    • parseFloat(this)

      Returns Constraint<number>

      Given an `<input />` element `inp_elem`
      var inp_val = cjs(inp_elem).toFloat();
    • parseInt(this, radix)

      Parameters

      • Optionalradix: unknown

      Returns Constraint<number>

      Given an `<input />` element `inp_elem`
      var inp_val = cjs(inp_elem).toInt();