DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 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-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • 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.

var canEvent = require("can-event");
var assign = require("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
var 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 Observation.add to signal when an an observable value has been read. The following makes the Person type’s getName() observable:

var Observation = require("can-observation");
var canEvent = require("can-event");
var assign = require("can-util/js/assign/assign");

// Create the Person type
function Person(){};
Person.prototype.setName = function(newName){
    var 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

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


var 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 various JavaScript and DOM related utilities.

DOM Utilities

The DOM utilities consist of:

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

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

can-view-callbacks

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

var callbacks = require("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.

var live = require("can-view-live");
var compute = require("can-compute");
var frag = require("can-util/dom/frag/frag");

var message = compute("World");

var 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.

var parser = require("can-view-parser");

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

var 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:

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

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

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

var globalScope = new Scope({message: "Hello"});
var outerScope = globalScope.add({last: "Abril"});
var 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.

var viewTarget = require("can-view-target");

var 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(){}]}

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

frag //-> <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.

var cid = require("can-cid");
var x = {};
var 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.

var types = require("can-types");
var 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.

var namespace = require('can-namespace');

var unicorn = {
    // ...
};

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

can-symbol

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

var MyIDSymbol = CanSymbol("my_ID");

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

can-reflect

can-reflect allows reflection on unknown data types.

var 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 3.14.1.

On this page

Get help

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