DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

Infrastructure

  • Edit on GitHub

Utility libraries that power the core and ecosystem collection.

Use

The infrastructure collection of libraries are lower-level utility libraries that are used by the Core and Ecosystem collections. They can also be used by applications directly.

Let’s explore what’s available.

can-event

[can-event] is a mixin that adds event dispatching and listening functionality on your objects. The following shows creating a Person constructor function whose instances can produce events that can be listened to.

import canEvent from 'can-event';
import assign from 'can-util/js/assign/assign';

// Create the Person type
function Person(){ /* ... */ };
Person.prototype.method = function(){ /* ... */ };

// Add event mixin:
assign(Person.prototype, canEvent);

// Create an instance
const me = new Person();

// Now listen and dispatch events!
me.addEventListener("name", function(){ /* ... */ });

me.dispatch("name");

[can-event/batch/batch] adds event batching abilities to the [can-event] event system. [can-event/async/async] adds asynchronous batched event dispatching to the [can-event] event system.

can-observation

can-observation provides a mechanism to notify when an observable has been read and a way to observe those reads called within a given function. can-observation provides the foundation for can-compute’s abilities.

Use [can-observation.add Observation.add] to signal when an an observable value has been read. The following makes the Person type’s getName() observable:

import Observation from 'can-observation';
import canEvent from 'can-event';
import assign from 'can-util/js/assign/assign';

// Create the Person type
function Person(){};
Person.prototype.setName = function(newName){
    let oldName = this.name;
    this.name = newName;
    this.dispatch("name", [newName, oldName]);
};
Person.prototype.getName = function(){
    Observation.add(this, "name");
    return this.name;
};

The Observation constructor can be used, similar to a can-compute to observe a function’s return value by tracking calls to Observation.add

const person = new Person();
person.setName("Justin");


const greetingObservation = new Observation(function(){
    return person.getName() + " says hi!";
}, null, function(newValue){
    console.log(newValue);
});
greetingObservation.start();

greetingObservation.value //-> "Justin says hi!"

person.setName("Matt") //-> console.logs "Matt says hi!";

can-util

[can-util] is a collection of many different modules that provide letious JavaScript and DOM related utilities.

DOM Utilities

The DOM utilities consist of:

  • Node and Element helpers: [can-util/dom/child-nodes/child-nodes], [can-util/dom/class-name/class-name], [can-util/dom/data/data], [can-util/dom/frag/frag].
  • Event helpers: [can-util/dom/dispatch/dispatch], [can-util/dom/events/delegate/delegate], [can-util/dom/events/attributes/attributes], [can-util/dom/events/inserted/inserted], [can-util/dom/events/removed/removed].
  • Ajax helpers: [can-util/dom/ajax/ajax].
  • Environment identification helpers: [can-util/dom/document/document].

And the [can-util/dom/mutate/mutate] helper which should be used to manipulate DOM nodes in elements that do not support MutationObservers.

JS Utilities

The JS utilities consist of:

  • Functional helpers: [can-util/js/each/each], [can-util/js/assign/assign], [can-util/js/deep-assign/deep-assign], [can-util/js/make-array/make-array].
  • Type detection helpers: [can-util/js/is-array-like/is-array-like], [can-util/js/is-empty-object/is-empty-object], [can-util/js/is-function], [can-util/js/is-plain-object/is-plain-object], [can-util/js/is-promise/is-promise], [can-util/js/is-string/is-string], [can-util/js/types/types].
  • Environment detection helpers: [can-util/js/is-browser-window/is-browser-window], [can-util/js/is-node/is-node], [can-util/js/is-web-worker/is-web-worker].
  • Environment identification helpers: [can-util/js/global/global], import, [can-util/js/base-url/base-url].
  • Polyfills - [can-util/js/set-immediate/set-immediate].
  • URL helpers: can-param, can-deparam, [can-util/js/join-uris/join-uris].
  • Diffing helpers: [can-util/js/diff/diff], [can-util/js/diff-object/diff-object].
  • String helpers: [can-util/js/string/string], [can-util/js/string-to-any/string-to-any].
  • Object identification helpers: [can-util/js/cid/cid].

can-view-callbacks

can-view-callbacks lets you register callbacks for specific elements or attributes found in templates.

import callbacks from 'can-view-callbacks';

callbacks.tag("blue-el", function(el){
    el.style.background = "blue";
});

can-view-live

Sets up a live-binding between the DOM and a compute.

import live from 'can-view-live';
import compute from 'can-compute';
import frag from 'can-util/dom/frag/frag';

let message = compute("World");

let content = frag("Hello","","!");

live.text(content.childNodes[1], message);

document.body.appendChild(content);

message("Earth");

document.body.innerHTML //-> Hello Earth!

can-view-nodelist

can-view-nodelist is used to maintain the structure of HTML nodes produced by a template. For example, a can-stache template like:

{{#if(over21)}}name:{{{highlight name}}}.{{/if}}

Might result in a nodeList structure that looks like:

if[
    TextNode("name:"),
    highlight[<b>Justin</b>]
]

This is to say that the #if over21 nodeList will contain a text node for "name:" and the highlight name nodeList. The highlight name nodeList will contain the html content resulting from that helper (<b>Justin</b>).

can-view-parser

can-view-parser parses HTML and handlebars/mustache tokens.

import parser from 'can-view-parser';

let html = '<h1><span first="foo"></span><span second="bar"></span></h1>';

let attrs = [];

parser(html, {
    attrStart: function(attrName){
        attrs.push(attrName)
    }
});

attrs //-> ["first", "second"]

can-view-scope

can-view-scope provides a lookup node within a contextual lookup. This is similar to a call object in closure in JavaScript. Consider how message, first, and last are looked up in the following JavaScript:

let message = "Hello"
function outer(){
    let last = "Abril";

    function inner(){
        let first = "Alexis";
        console.log(message + " "+ first + " " + last);
    }
    inner();
}
outer();

can-view-scope can be used to create a similar lookup path:

let globalScope = new Scope({message: "Hello"});
let outerScope = globalScope.add({last: "Abril"});
let innerScope = outerScope.add({first: "Alexis"});
innerScope.get("message") //-> Hello
innerScope.get("first")   //-> Alexis
innerScope.get("last")    //-> Abril

can-view-target

can-view-target is used to create a document fragment that can be quickly cloned but have callbacks called quickly on specific elements within the cloned fragment.

import viewTarget from 'can-view-target';

let target = viewTarget([
    {
        tag: "h1",
        callbacks: [function(data){
            this.className = data.className
        }],
        children: [
            "Hello ",
            function(){
                this.nodeValue = data.message
            }
        ]
    },
]);

// target.clone -> <h1>|Hello||</h1>
// target.paths -> path: [0], callbacks: [], children: {paths: [1], callbacks:[function(){}]}

let fragment = target.hydrate({className: "title", message: "World"});

// fragment -> <h1 class='title'>Hello World</h1>

can-cid

can-cid is used to get a unique identifier for an object, optionally prefixed by a type name. Once set, the unique identifier does not change, even if the type name changes on subsequent calls.

import cid from 'can-cid';
const x = {};
const y = {};

console.log(cid(x, "demo")); // -> "demo1"
console.log(cid(x, "prod")); // -> "demo1"
console.log(cid(y));         // -> "2"

can-types

can-types is used to provide default types or test if something is of a certain type.

import types from 'can-types';
let oldIsMapLike = types.isMapLike;
types.isMapLike = function(obj){
  return obj instanceof DefineMap || oldIsMapLike.apply(this, arguments);
};
types.DefaultMap = DefineMap;

can-namespace

can-namespace is a namespace where can-* packages can be registered.

import namespace from 'can-namespace';

const unicorn = {
    // ...
};

if (namespace.unicorn) {
    throw new Error("You can't have two versions of can-unicorn, check your dependencies");
}

export default namespace.unicorn = unicorn;

can-symbol

can-symbol contains Symbols used to detail how CanJS may operate on different objects.

let MyIDSymbol = CanSymbol("my_ID");

const obj = {};
obj[MyIDSymbol] = 1;

can-reflect

can-reflect allows reflection on unknown data types.

const foo = new DefineMap({ bar: "baz" });

canReflect.getKeyValue(foo, "bar"); // -> "baz"

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 5.33.3.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news