DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.6.1
5.33.3 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-deep-observable
      • 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-observable-bindings
      • 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-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • 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
    • 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-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-queue

can-event-queue/map/map 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 { mapEventBindings } from "can";

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

// Add event mixin:
mapEventBindings(Person.prototype);

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

// Now listen and dispatch events!
me.addEventListener("name", function(ev,data){ console.log(data) }); // -> {foo: "Bar"}

me.dispatch("name", [{foo: "Bar"}]);

can-queues

A light weight queue system for scheduling tasks, it runs tasks in one of the following queues:

  1. notifyQueue - Tasks that notify "deriving" observables that a source value has changed.
  2. deriveQueue - Tasks that update the value of a "deriving" observable.
  3. domUIQueue - Tasks that update the DOM.
  4. mutateQueue - Tasks that might cause other mutations that add tasks to one of the previous queues.
import { queues } from "can";

queues.batch.start();
queues.mutateQueue.enqueue( console.log, console, [ "say hi" ] );
queues.batch.stop();

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 ObservationRecorder.add to signal when an an observable value has been read. The following makes the Person type’s getName() observable:

import { Observation, ObservationRecorder, mapEventBindings } from "can";

// Create the Person type
function Person(){};

// Add event mixin:
mapEventBindings(Person.prototype);

Person.prototype.setName = function(newName){
    let oldName = this.name;
    this.name = newName;
    this.dispatch("name", [newName, oldName]);
};
Person.prototype.getName = function(){
    ObservationRecorder.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

import { Observation, ObservationRecorder, mapEventBindings } from "can";

// Create the Person type
function Person(){};

// Add event mixin:
mapEventBindings(Person.prototype);

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

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


const greetingObservation = new Observation(function(){
    return person.getName() + " says hi!";
});

ObservationRecorder.start();

greetingObservation.on(function(newValue) {
  console.log(newValue);
});

console.log(greetingObservation.value); //-> "Justin says hi!"

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

Utilities

DOM Utilities

The DOM utilities consist of:

  • Node and Element helpers: can-child-nodes, can-dom-data, can-fragment.
  • Event helpers: can-event-dom-enter, can-event-dom-radiochange.
  • Ajax helpers: can-ajax.
  • Environment identification helpers: document.

And the can-dom-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-assign, can-define-lazy-value, can-make-map.
  • Type detection helpers: isConstructorLike, isFunctionLike, isIteratorLike,isListLike, isMapLike, ,isObservableLike, isPlainObject, isPromise, isPrimitive, can-types.
  • Environment detection helpers: is-browser-window, is-node, [ccan-globals/is-browser-window/is-web-worker].
  • Environment identification helpers: can-global, can-globals.
  • URL helpers: can-param, can-deparam, can-join-uris.
  • Diffing helper: can-diff.
  • String helpers: can-string, can-string-to-any.
  • Object identification helpers: can-cid.

can-view-callbacks

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

import { stache, viewCallbacks } from "can";

viewCallbacks.tag("blue-el", function(el, tagData) {
    el.style.background = "blue";
    var frag = tagData.subtemplate( {
        message: "Hello"
    }, tagData.options );
  
   el.appendChild(frag);
});

var view = stache("<blue-el>{{ message }}</blue-el>");

var frag = view();

document.body.appendChild(frag);

can-view-live

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

import { compute, fragment, viewLive } from "can";

let message = compute("World");

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

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

document.body.appendChild(content);

message("Earth");

console.log(document.body.innerHTML); //-> Hello Earth

can-view-parser

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

import { viewParser } from "can";

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

let attrs = [];

viewParser(html, {
    attrStart: function(attrName){
        attrs.push(attrName)
    },
    attrEnd: function() {},
    start: function() {},
    end: function() {},
    attrValue: function() {},
    done: function() {}
});

console.log(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:

import { Scope } from "can";

let globalScope = new Scope({message: "Hello"});
let outerScope = globalScope.add({last: "Abril"});
let innerScope = outerScope.add({first: "Alexis"});
console.log(innerScope.get("../../message")); //-> Hello
console.log(innerScope.get("first"));   //-> Alexis
console.log(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 { target } from "can";

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

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

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

console.log(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";
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-reflect

can-reflect allows reflection on unknown data types.

import { ObservableObject, Reflect as canReflect } from "can";

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

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

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

On this page

Get help

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