DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
    • can-construct
    • can-control
    • can-event
    • can-event/async/async
    • can-event/batch/batch
    • can-observation
    • can-simple-map
    • can-util
    • can-view-callbacks
    • can-view-live
    • can-view-model
    • can-view-nodelist
    • can-view-parser
    • can-view-scope
    • can-view-target
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Infrastructure
  • / On this page
    • Infrastructure

      page

      Utility libraries that power the core and ecosystem collection.

      • source

      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.

      Lets 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: childNodes, className, data, frag.
      • Event helpers: dispatch, delegateEvents, attributes, inserted, removed.
      • Ajax helpers: ajax.
      • Environment identification helpers: 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, deepAssign, makeArray.
      • Type detection helpers: isArrayLike, isEmptyObject, /is-function isFunction, isPlainObject, isPromise, isString, can-util/js/types/types.
      • Environment detection helpers: isBrowserWindow, isNode, isWebWorker.
      • Environment identification helpers: global, import, baseUrl.
      • Polyfills - setImmediate.
      • URL helpers: param, deparam, joinURIs.
      • Diffing helpers: diff, diffObject.
      • 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>
      

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