Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: objectvar x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: Omit<ArrayConstraintOptions<any>, "value">var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: Omit<ArrayConstraintOptions<NoInfer<T>>, "value">var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: ConstraintOptions<any>var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: ConstraintOptions<NoInfer<T>>var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: ConstraintOptions<NoInfer<T>>var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: Omit<MapConstraintOptions<string, any>, "value">var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionaloptions: Omit<MapConstraintOptions<string, NoInfer<V>>, "value">var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates a constraint of the right kind for value: an ArrayConstraint for an array,
a MapConstraint for a plain object, a constraint for an input element's value for a DOM
node, and otherwise a Constraint (whose value can be a plain value, a function that
computes it, or another constraint to follow).
Optionalvalue: TOptionaloptions: ConstraintOptions<NoInfer<T>>var x = cjs(),
y = cjs(1),
z = cjs(function() {
return y.get() + 1;
});
x.get(); // undefined
y.get(); // 1
z.get(); // 2
Options
var yes_lit = cjs(function() { return 1; }, { literal: true }),
not_lit = cjs(function() { return 1; }, { literal: false });
yes_lit.get(); // (function)
not_lit.get(); // 1
Creates an array constraint.
Computes how to transform from into to with as few operations as practical: which items
were removed, which were added, and which have to move. This is used to keep DOM nodes in sync
with a model while touching as few of them as possible.
How to tell whether two items are the same (default: ===)
cjs.arrayDiff(["a", "b", "c"], ["c", "b", "d"]);
// {
// removed: [{ from: 0, from_item: "a" }],
// added: [{ item: "d", to: 2, to_item: "d" }],
// moved: [{ item: "c", from: 2, to: 0, move_from: 1, insert_at: 0 }],
// index_changed: [{ item: "c", from: 2, to: 0, from_item: "c", to_item: "c" }]
// }
Constrain a DOM element's attributes, given an object of attribute names and values (which can
be a constraint or map constraint), or an attribute name and a value. An attribute is removed
when its value is null or undefined (or, for boolean attributes like disabled, falsy).
Constrain a DOM element's children. Each value can be a node, text, or an array of them. The bound children are kept at the start of the element; any children it already had stay after them.
Constrain a DOM element's class names. Each value can be a class name, a space-separated list of class names, or an array of them. Classes that the element had before (and that aren't in the values) are left alone.
Constrain a DOM element's CSS styles, given an object of property names and values (which can be a constraint or map constraint), or a property name and a value.
Constrain a DOM element's HTML content to the concatenation of values.
Constrain a DOM node's text content to the concatenation of values.
Constrain an input element's value to the concatenation of values.
Creates a constraint.
Optionalvalue: ConstraintSource<T>
The initial value, a function to compute it, or a constraint to follow
Optionaloptions: ConstraintOptions<T>Parses an expression (with the same syntax as template expressions) and returns a constraint
for its value. Names in the expression are looked up on context (an object or map
constraint). If the expression can't be parsed or evaluated, the error is logged and the
value is undefined.
The expression (or a constraint whose value is one)
Creates a template. With a context, renders it right away and returns the resulting DOM
node. Otherwise, returns a function that can be called with a context (and optionally a parent
node to render into) to render it.
ConstraintJS templates use a syntax like Handlebars. Values in the context can be constraints; the rendered DOM stays up to date as they change.
<h1>{{title}}</h1>
<p>{{subtext.toUpperCase() + "!"}}</p>
With { title: cjs('hello'), subtext: 'world' }, renders <h1>hello</h1><p>WORLD!</p>.
Use triple braces to insert HTML: <p>{{{html_content}}}</p>
Comments are ignored: {{! a comment }}
data-cjs-on-(event)=handler calls the context's handler when the event happens:
<div data-cjs-on-click=update_obj></div>
data-cjs-out=name sets the context's name to a constraint for the input's value:
<input data-cjs-out=user_name />
<h1>Hello, {{user_name}}</h1>
{{#each}} renders its content for every item of an array or object. {{this}} is the
current item, {{@index}} its index, and for objects, {{@key}} its key. The {{#else}}
part is rendered when there are no items.
{{#each arr_name}}
{{@index}}: {{this}}
{{#else}}
<strong>No items!</strong>
{{/each}}
{{#if cond1}}
Cond content
{{#elif other_cond}}
other_cond content
{{#else}}
else content
{{/if}}
The opposite of {{#if}} is {{#unless}}:
{{#unless logged_in}}
Not logged in!
{{/unless}}
{{#fsm}} renders the content for an FSM's current state:
{{#fsm my_fsm}}
{{#state state1}}
State1 content
{{#state state2}}
State2 content
{{/fsm}}
{{#with}} changes the context. {{../x}} reads x from the enclosing context.
{{#with obj}}
Value: {{x}}
{{/with}}
Partials nest templates in other templates:
var my_temp = cjs.createTemplate(...);
cjs.registerPartial('my_template', my_temp);
Then, in any other template, {{>my_template context}} renders my_template with context.
The template, or an element (like a <script type="cjs/template">) whose text is the template
Creates a template. With a context, renders it right away and returns the resulting DOM
node. Otherwise, returns a function that can be called with a context (and optionally a parent
node to render into) to render it.
ConstraintJS templates use a syntax like Handlebars. Values in the context can be constraints; the rendered DOM stays up to date as they change.
<h1>{{title}}</h1>
<p>{{subtext.toUpperCase() + "!"}}</p>
With { title: cjs('hello'), subtext: 'world' }, renders <h1>hello</h1><p>WORLD!</p>.
Use triple braces to insert HTML: <p>{{{html_content}}}</p>
Comments are ignored: {{! a comment }}
data-cjs-on-(event)=handler calls the context's handler when the event happens:
<div data-cjs-on-click=update_obj></div>
data-cjs-out=name sets the context's name to a constraint for the input's value:
<input data-cjs-out=user_name />
<h1>Hello, {{user_name}}</h1>
{{#each}} renders its content for every item of an array or object. {{this}} is the
current item, {{@index}} its index, and for objects, {{@key}} its key. The {{#else}}
part is rendered when there are no items.
{{#each arr_name}}
{{@index}}: {{this}}
{{#else}}
<strong>No items!</strong>
{{/each}}
{{#if cond1}}
Cond content
{{#elif other_cond}}
other_cond content
{{#else}}
else content
{{/if}}
The opposite of {{#if}} is {{#unless}}:
{{#unless logged_in}}
Not logged in!
{{/unless}}
{{#fsm}} renders the content for an FSM's current state:
{{#fsm my_fsm}}
{{#state state1}}
State1 content
{{#state state2}}
State2 content
{{/fsm}}
{{#with}} changes the context. {{../x}} reads x from the enclosing context.
{{#with obj}}
Value: {{x}}
{{/with}}
Partials nest templates in other templates:
var my_temp = cjs.createTemplate(...);
cjs.registerPartial('my_template', my_temp);
Then, in any other template, {{>my_template context}} renders my_template with context.
The template, or an element (like a <script type="cjs/template">) whose text is the template
Optionalparent: unknownCreates an FSM.
Gets the value of anything: the value of a constraint, a plain array for an array constraint, a plain object for a map constraint, or the object itself for anything else.
OptionalautoAddOutgoing: boolean
For constraints, whether a constraint that is computing its value right
now should start depending on this one (default: true)
Gets the value of anything: the value of a constraint, a plain array for an array constraint, a plain object for a map constraint, or the object itself for anything else.
Gets the value of anything: the value of a constraint, a plain array for an array constraint, a plain object for a map constraint, or the object itself for anything else.
Gets the value of anything: the value of a constraint, a plain array for an array constraint, a plain object for a map constraint, or the object itself for anything else.
Creates a constraint whose value depends on the state of an FSM.
For each state name, the constraint's value in that state
A constraint whose value is the value of an input element (or, given several inputs, an array of their values).
Whether value is an array constraint.
Whether value is a constraint.
Whether value is a map constraint.
Runs func, and runs it again whenever any constraint it read changes.
Optionaloptions: LivenOptionsCreates a map constraint.
Memoizes a function: its result is remembered for each set of arguments, and only recomputed when a constraint that it read (for those arguments) changes.
Optionaloptions: MemoizeOptionsvar arr = cjs([3,2,1,4,5,10]),
get_nth_largest = cjs.memoize(function(n) {
console.log('recomputing');
var sorted_arr = arr.sort(function(a, b) { return b - a; });
return sorted_arr[n];
});
get_nth_largest(0); // logged: recomputing
get_nth_largest(0); // (nothing logged because the answer is memoized)
arr.splice(0, 1);
get_nth_largest(0); // logged: recomputing
Creates an event for FSM transitions (see FSM.prototype.addTransition).
The type of event to listen for, like "click" or "timeout" (several
types can be separated by spaces). It can be a constraint.
What to listen to (default: window). For "timeout", the delay in
milliseconds instead.
Removes the dependency of to on from (until to reads from again).
Tells the constraint solver it is ready to run any onChange listeners. signal needs to be
called the same number of times as wait before the listeners will run.
"ConstraintJS v" + cjs.version
The version of ConstraintJS.
Tells the constraint solver to delay running any onChange listeners.
Note that signal needs to be called the same number of times as wait before the listeners
will run.
Stops a rendered template from updating and cleans up after it.
A DOM node returned by a template function
Restores the previous value of the global cjs variable (when ConstraintJS was loaded with a
<script> tag) and returns ConstraintJS.
Pauses updates to a rendered template (until resumeTemplate).
A DOM node returned by a template function
Registers a custom partial, whose DOM node is created by a function, that other templates
can use ({{>name args...}}).
Registers a template as a partial that other templates can use ({{>name context}}).
Resumes updates to a rendered template (after pauseTemplate).
A DOM node returned by a template function
ConstraintJS. Calling
cjs(value)creates a constraint; every other API is a property ofcjs.