Loading
using-loading
There are several ways to load CanJS in your application. Most likely you will use one of the pre-built versions like can.jquery.js
but it is also possible to load each module individually using AMD or Steal.
In a
<script>
tagThe most common way of using CanJS is the core builds from the download (or download builder) included in a
<script>
tag. The following section contains quick instructions how to load CanJS for each library it supports.jQuery
CanJS supports jQuery in the latest 1.X and 2.0 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:
AMD
The CanJS Download contains an
amd/
folder which allows you to load any CanJS component and plugin using an AMD module loader like RequireJS. Unlike many other JavaScript libraries CanJS is fully modular and loading a dependency will only include the modules actually used (for example,can/control
will only loadcan/construct
andcan/util
). Theamd-dev/
folder contains the same files but with debugging message enabled which can be very helpful during development.Configuration
The default library used with AMD modules is jQuery. CanJS will reference the
jquery
module so that name needs to point to your jQuery source and thecan
module name needs to be mapped to theamd/
folder of the CanJS download.RequireJS
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
:Using 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:
StealJS
StealJS is the dependency manager that comes with JavaScriptMVC and that is natively used by CanJS. Since JavaScriptMVC comes with CanJS and Steal, the easiest way to use them together is by downloading JavaScriptMVC. You can also use the
steal/
folder from the CanJS download or Bower package.With the JavaScriptMVC download, in the main folder, you can simply run the application generator:
In
app/app.js
you should see something like:This file will be loaded when opening
app/index.html
and you are ready to use CanJS with StealJS and make production builds. For more information follow up in the JavaScriptMVC documentation.