Setting up CanJS
Using
CanJS is packaged in multiple ways so that it can fit into any development workflow. Learn how to setup CanJS in different environments.
CanJS is packaged in multiple ways so that it can fit into any development workflow. Learn how to setup CanJS in different environments.
CanJS can be downloaded or installed in many ways:
Once downloaded or installed, CanJS can be loaded in a variety of ways:
<script>
tagsThe following lists how to setup common combinations. If you don't see yours, please ask on the forums or gitter chat.
JSBins
Not yet committed to CanJS? Or just want to play around? Use one of these JSBins:
JS Bin on jsbin.com
CDN
Another quick way to start locally is by loading scripts from our CDN:
This loads CanJS's core and its two most common plugins define and stache. Checkout the release folder for the list of other plugins you can add.
If you want to use Zepto instead of jQuery, load Zepto instead of jQuery and
can.zepto.js
instead ofcan.jquery.js
like:A list of all available CDN releases and files can be found here.
Note: We highly recommend to always reference a specific version and never
latest
directly in a production environment. Latest can contain backwards incompatible releases and will break your application.StealJS and NPM
If you are installing CanJS from npm and using StealJS, you should checkout DoneJS. It has comprehensive guides on using StealJS and CanJS together.
To use StealJS and CanJS outside DoneJS, install the can, steal and
jquery
packages:Next, create a
main.stache
template for your app:Next, create a
main
module for your application. Import CanJS's core, jQuery, and your template to say "Hello World":Finally, create a page that loads
steal.js
and specifies"main"
as the main module:It's better if you import just what you need. Instead of loading all of CanJS's core, we can load just
"can/map/map"
like:StealJS supports "modlet" module names that end with "/". This means that the above could also be written like:
Besides ES6 modules, StealJS supports AMD, and CommonJS. You could also write
main.js
like:Building to production
If you are using StealJS and CanJS outside of DoneJS, this guide walks through getting a production build.
Browserify and NPM
The can npm package works with browserify. After installing
can
:Require the core
can
modules like:You can also
require
specific modules:Note that nearly all module names repeat the folder name (ex:
can/view/stache/stache
).Requiring templates
You can require any CanJS templates using the can-compilify Browserify transform. Complete instructions for installing and using the transform are available on npm.
Install can-compilify:
Require a template in your code:
And include the can-compilify transform from the command line:
You could also add can-compilify to your package.json.
Building templates to production
Using the can-compilify tranform, your production bundle(s) will include a compiled version of your templates so no extra setup is required.
RequireJS from NPM, the Download or Bower
The Zip Download (on the homepage) contains the following AMD formatted scripts that can be loaded with RequireJS.
amd/
- CanJS provided as AMD modulesamd-dev/
- CanJS AMD modules with development messagesThese files are also available in the can NPM package's
dist
folder. Install thecan
package like:You'll find these files in
node_modules/can/dist
.These files are also available in the
canjs
bower package. Installcanjs
like:You'll find these files in
bower_components/can/dist
.The following section contains quick how to load CanJS with RequireJS for different libraries.
jQuery
In RequireJS a simple configuration looks like this:
The
can
module is a shortcut that loads CanJS's core plugins and returns thecan
namespace:Dojo
The configuration for Dojo is similar but
can/util/library
needs to be mapped tocan/util/dojo
:Other libraries
If you would like to use another library, map the
can/util/library
module tocan/util/zepto
,can/util/yui
orcan/util/mootools
.With RequireJS and Zepto, it loks like this:
Building templates to production
Bundling templates with the rest of your JavaScript can dramatically reduce the number of requests.
Use the can-compile project to build templates into an AMD module that can be used by
r.js
to build the app.Script tags from NPM, the Download, or Bower
The Zip Download (on the homepage) contains the following "global" scripts that can be loaded with just a
<script>
tag.can.<library>.js
(e.g.can.jquery.js
) - The core build for a supported librarycan.<library>.dev.js
- A development build logging useful messages for a supported librarycan.<library>.min.js
- The minified core build for a supported librarycan.<type>.<plugin>
- Individual builds for each official CanJS pluginThese are also available in the can NPM package's
dist
folder. Install thecan
package like:You'll find these files in
node_modules/can/dist
.These files are also available in the
canjs
bower package. Installcanjs
like:You'll find these files in
bower_components/can/dist
.The following section contains quick how to load CanJS with a
<script>
tag using these files for each library it supports.jQuery
CanJS supports jQuery in the latest 1.X and 2.X version. Include jQuery before your CanJS jQuery build to get started:
Zepto
CanJS supports Zepto 0.9+. Include a copy of Zepto along with CanJS to get started.
Dojo
You can also use the Dojo base download and simply include it alongside CanJS:
Mootools
CanJS supports Mootools 1.4+. Include a copy of Mootools Core along with CanJS to get started.
Mootools Core has an issue where focus and blur events are not fired for delegate event listeners. Include Mootools More's Event.Pseudos module for focus and blur support.
YUI
CanJS supports YUI 3.4+ with both dynamically or statically loaded modules. CanJS depends on the following YUI modules: node, io-base, querystring, event-focus, and array-extras. The selector-css2 and selector-css3 YUI modules are optional, but necessary for IE7 and other browsers that don't support querySelectorAll.
To use with dynamically loaded modules, include the YUI loader along with CanJS. Add
'can'
to your normal list of modules withYUI().use('can', ...)
wherever CanJS will be used.To use with statically loaded modules, include a static copy of YUI (with the previously mentioned YUI dependencies) along with CanJS. CanJS will automatically be included wherever
YUI().use('*')
is used.CanJS can also bind to YUI widget events. The following example shows how to bind to the selectionChange event for a YUI Calendar widget:
Building templates to production
CanJS can pre-compile EJS, Mustache, and Stache views into JavaScript functions contained within a single file in order to avoid additional requests for view files in production. When using CanJS standalone use the can-compile Node module. Install with
And in your project root folder run:
This will create
views.production.js
in the current folder containing all pre-compiled views. When loaded into your page CanJS will use the per-compiled views instead of making an Ajax request to retrieve them.Script tags with the download builder
The download builder on the homepage lets you customize what modules are included in a global script. After downloading that script, the use of that script is just like loading CanJS with script tag using the zip download.