can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
893 lines (625 loc) • 26.9 kB
HTML
<!--####################################################################
THIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN
INSTEAD CHANGE:
source: [object Object]
@page guides/setup
######################################################################## -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>CanJS - Setting up CanJS</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" type="text/css" href="../static/bundles/bit-docs-site/static.css">
<link rel="shortcut icon" sizes="16x16 24x24 32x32 48x48 64x64" href="/docs/images/canjs_favicon.ico">
<link rel="apple-touch-icon" sizes="57x57" href="../../docs/images/canjs_favicon_57x57.png">
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="../../docs/images/canjs_favicon_57x57.png">
<link rel="apple-touch-icon" sizes="72x72" href="../../docs/images/canjs_favicon_72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="../../docs/images/canjs_favicon_114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="../../docs/images/canjs_favicon_128x128.png">
<link rel="apple-touch-icon" sizes="144x144" href="../../docs/images/canjs_favicon_144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="../../docs/images/canjs_favicon_152x152.png">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta name="apple-mobile-web-app-status-bar-style" content="white-translucent">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2302003-11', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger"/>
<label for="nav-trigger">Menu</label>
<div id="everything">
<div id="left" class="column">
<div class="top-left">
<div class="brand">
<div class="logo">
<a href="../../index.html" alt="CanJS"></a>
<div class="dropdown project-dropdown">
<a href="https://donejs.com/">DoneJS</a>
<a href="http://stealjs.com/">StealJS</a>
<a href="http://jquerypp.com/">jQuery ++</a>
<a href="https://funcunit.com/">FuncUnit</a>
<a href="http://documentjs.com/">DocumentJS</a>
</div>
</div>
<div class="version">
<div class="version-number">
3.0.0
</div>
<div class="dropdown version-dropdown">
<a href="https://v2.canjs.com">2.3.27</a>
</div>
</div>
</div>
<div class="search-bar">
<p>
</p>
</div>
</div>
<div class="bottom-left">
<div class="social-side-container">
<ul class="social-side">
<li>
<a class="header-mobile github" href="https://github.com/canjs/canjs" target="_blank"><img class="social-icon-small" src="../../docs/images/github.png">Github</a>
</li>
<li>
<a class="header-mobile twitter" href="https://twitter.com/canjs" target="_blank"><img class="social-icon-small" src="../../docs/images/twitter.png">Twitter</a>
</li>
</ul>
<ul class="social-side">
<li>
<a class="header-mobile" href="https://gitter.im/canjs/canjs" target="_blank">Chat</a>
</li>
<li>
<a class="header-mobile" href="http://forums.donejs.com/c/canjs" target="_blank">Forum</a>
</li>
</ul>
</div>
<ul>
<li class="
parent
expanded">
<a class="page"
href="../guides.html"
title="Welcome to CanJS! These guides are here to help you develop and improve your relationship with CanJS. After all, picking a JavaScript framework is a commitment. We want CanJS to be the
framework you marry. This page helps you know how advance through the different stages of this
relationship:">
Guides
</a>
<ul>
<li>
<span>introduction</span>
<ul>
<li class="
">
<a class="page"
href="mission.html"
title="Learn about CanJS's mission, why it matters, and how we've worked (and will keep working) to accomplish it.">
Mission
</a>
</li>
<li class="
">
<a class="page"
href="technical.html"
title="">
Technical Highlights
</a>
</li>
<li class="
">
<a class="page"
href="who-uses-canjs.html"
title="">
Who uses CanJS?
</a>
</li>
</ul>
</li>
<li>
<span>experiment</span>
<ul>
<li class="
">
<a class="page"
href="chat.html"
title="This guide walks through building real time chat application with CanJS's Core libraries. It takes about 30 minutes to complete.">
Chat Guide
</a>
</li>
<li class="
">
<a class="page"
href="todomvc.html"
title="This guide walks through building a slightly modified version of TodoMVC with CanJS's Core libraries and can-fixture. It takes about 1 hour to complete.">
TodoMVC Guide
</a>
</li>
<li class="
">
<a class="page"
href="atm.html"
title="This guide walks through building and testing an ATM application with CanJS's
Core libraries. It teaches how to do test driven development (TDD)
and manage complex state. It takes about 2 hours to complete.">
ATM Guide
</a>
</li>
<li class="current
parent
expanded">
<a class="page"
href="setup.html"
title="CanJS is packaged in multiple ways so that it can fit into any development workflow. Learn how to setup CanJS in different environments.">
Setting up CanJS
</a>
</li>
</ul>
</li>
<li>
<span>commitment</span>
<ul>
<li class="
">
<a class="page"
href="api.html"
title="This page walks through how to use and understand CanJS's API documentation.">
API Guide
</a>
</li>
<li class="
">
<a class="page"
href="examples.html"
title="">
Examples
</a>
</li>
<li class="
">
<a class="page"
href="../roadmap.html"
title="Learn about CanJS's future plans and how we make them, and how you can influence them.">
Roadmap
</a>
</li>
<li class="
">
<a class="page"
href="../migrate-3.html"
title="">
Migrating to 3.0
</a>
</li>
</ul>
</li>
<li>
<span>contribute</span>
<ul>
<li class="
">
<a class="page"
href="contributing/bug-report.html"
title="Learn how to submit a bug report.">
Bug Report
</a>
</li>
<li class="
">
<a class="page"
href="contributing/code.html"
title="Learn how contribute a code change to CanJS.">
Code
</a>
</li>
<li class="
">
<a class="page"
href="contributing/documentation.html"
title="Learn how to improve CanJS's site and documentation.">
Documentation
</a>
</li>
<li class="
">
<a class="page"
href="contributing/evangelism.html"
title="Learn about resources that can help you spread the word about CanJS.">
Evangelism
</a>
</li>
<li class="
">
<a class="page"
href="contributing/feature-suggestion.html"
title="Learn how to suggest a feature.">
Feature Suggestion
</a>
</li>
<li class="
">
<a class="page"
href="contributing/releases.html"
title="Release and hosting information for CanJS maintainers.">
Releases
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="
">
<a class="page"
href="../can-core.html"
title="The best, most hardened and generally useful libraries in CanJS.">
Core
</a>
</li>
<li class="
">
<a class="page"
href="../can-ecosystem.html"
title="Useful libraries that extend or add important features to the core collection.">
Ecosystem
</a>
</li>
<li class="
">
<a class="page"
href="../can-infrastructure.html"
title="Utility libraries that power the core and ecosystem collection.">
Infrastructure
</a>
</li>
<li class="
">
<a class="page"
href="../can-legacy.html"
title="Former libraries that we still accept patches for, but are not under active development.">
Legacy
</a>
</li>
</ul>
</div>
</div>
<div id="right" class="column">
<div class="top-right">
<div class="top-right-top">
<ul class="top-right-bitovi">
<li class="dropdown">
<a href="http://bitovi.com" class="bitovi icon-bits">Bitovi</a>
<ul class="dropdown-menu">
<li><a href="http://bitovi.com">Bitovi.com</a></li>
<li><a href="http://bitovi.com/blog/">Blog</a></li>
<li><a href="http://bitovi.com/consulting/">Consulting</a></li>
<li><a href="http://bitovi.com/training/">Training</a></li>
<li><a href="http://bitovi.com/open-source/">Open Source</a></li>
</ul>
</li>
</ul>
<div class="brand">
<div class="logo">
<a href="../../index.html" alt="CanJS"></a>
</div>
</div>
<ul class="top-right-links">
<li>
<a href="https://gitter.im/canjs/canjs">Chat</a>
</li>
<li>
<a href="http://forums.donejs.com/c/canjs">Forum</a>
</li>
<li>
<a class="github-button nav-social" href="https://github.com/canjs/canjs" data-count-href="/canjs/canjs/stargazers" data-count-api="/repos/canjs/canjs#stargazers_count">Star</a>
</li>
<li>
<a href="https://twitter.com/canjs" class="twitter-follow-button nav-social" data-show-count="true" data-show-screen-name="false">Follow @canjs</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</li>
</ul>
</div>
<div class="breadcrumb">
<li><a href="../../index.html">CanJS</a></li> /
<li><a href="../guides.html">Guides</a></li> /
<li><a href="setup.html">Setting up CanJS</a></li>
<li class="breadcrumb-dropdown">/ <a> On this page</a>
<ul class="on-this-page"></ul>
</li>
<div class="nav-toggle" title="Back to top"></div>
</div>
</div>
<div class="bottom-right">
<article>
<section class="title">
<div class="page-type">
<h1>Setting up CanJS</h1>
<div>page</div>
</div>
<section class="description">
<p>CanJS is packaged in multiple ways so that it can fit into any development workflow. Learn how to setup CanJS in different environments.</p>
</section>
</section>
<section class="on-this-page-table">
</section>
<section class="title-footer">
<ul class="title-links">
<!-- <li><a href="#">docco</a></li> -->
<li><a href="//github.com/canjs/canjs/tree/v3.0.0/docs/can-guides/experiment/setting-up-canjs.md">source</a></li>
<!-- <li><a href="#">download</a></li> -->
<!-- <li><a href="#">tests</a></li> -->
</ul>
</section>
<section class="body">
<p>CanJS can be downloaded or installed in many ways:</p>
<ul>
<li>npm</li>
<li>the zip download (on the homepage)</li>
<li>the download builder (on the homepage)</li>
<li>loaded from our cdn</li>
<li>bower</li>
</ul>
<p>Once downloaded or installed, CanJS can be loaded in a variety of ways:</p>
<ul>
<li>StealJS,</li>
<li>RequireJS</li>
<li>Browserify</li>
<li><code><script></code> tags</li>
</ul>
<p>The following lists how to setup common combinations. If you don't see yours, please
ask on the <a href="http://forums.donejs.com/c/canjs">forums</a> or <a href="https://gitter.im/canjs/canjs">gitter chat</a>.</p>
<h2>JSBins</h2>
<p>Not yet committed to CanJS? Or just want to play around? Use one of these JSBins:</p>
<p><a class="jsbin-embed" href="http://jsbin.com/venaje/embed?html,js,output">JS Bin on jsbin.com</a><script src="//static.jsbin.com/js/embed.min.js?3.35.5"></script></p>
<ul>
<li><a href="http://justinbmeyer.jsbin.com/venaje/edit?html,js,output">jQuery</a></li>
<li><a href="http://justinbmeyer.jsbin.com/veqola/edit?html,js,output">Zepto</a></li>
</ul>
<h2>CDN</h2>
<p>Another quick way to start locally is by loading scripts from our CDN:</p>
<pre><code><html>
<head>
<title>CanJS Test</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<script src="http://canjs.com/release/latest/can.jquery.js"></script>
<script src="http://canjs.com/release/latest/can.map.define.js"></script>
<script src="http://canjs.com/release/latest/can.stache.js"></script>
<script type='text/stache' id='app'>
<hello-world/>
</script>
<script type="text/javascript">
can.Component.extend({
tag: 'hello-world',
template: can.stache("<h1>{{message}}</h1>"),
viewModel: {
message: "Hi there!"
}
});
$("body").append(can.view("app",{}));
</script>
</body>
</html>
</code></pre>
<p>This loads CanJS's core and its two most common plugins <a href="../docs/can.Map.prototype.define.html">define</a>
and <a href="../docs/can.stache.html">stache</a>. Checkout the <a href="https://github.com/bitovi/canjs.com/tree/gh-pages/release/latest">release folder</a>
for the list of other plugins you can add.</p>
<p>If you want to use Zepto instead of jQuery, load Zepto instead of jQuery and <code>can.zepto.js</code> instead of <code>can.jquery.js</code> like:</p>
<pre><code><script src="http://zeptojs.com/zepto.js"></script>
<script src="http://canjs.com/release/latest/can.zepto.js"></script>
</code></pre>
<p>A list of all available CDN releases and files can be found <a href="https://github.com/canjs/canjs.com/tree/gh-pages/release">here</a>.</p>
<p><strong>Note:</strong> We highly recommend to always reference a specific version and never <code>latest</code> directly in a production environment.
Latest can contain backwards incompatible releases <strong>and will break your application</strong>.</p>
<h2>StealJS and NPM</h2>
<p>If you are installing CanJS from npm and using StealJS, you should
checkout <a href="http://donejs.com">DoneJS</a>. It has comprehensive guides on using
StealJS and CanJS together.</p>
<p>To use StealJS and CanJS outside DoneJS, install the <a href="https://www.npmjs.com/package/can">can</a>, <a href="https://www.npmjs.com/package/steal">steal</a> and <code>jquery</code> packages:</p>
<pre><code>> npm install can --save
> npm install steal --save
> npm install jquery --save
</code></pre>
<p>Next, create a <code>main.stache</code> template for your app:</p>
<pre><code><!-- main.stache -->
<h1>{{message}}</h1>
</code></pre>
<p>Next, create a <code>main</code> module for your application. Import CanJS's core,
jQuery, and your template to say "Hello World":</p>
<pre><code>// main.js
import can from "can";
import $ from "jquery";
import template from "./main.stache!";
var data = new can.Map({message: "Hello World"});
$("body").append(template(data));
</code></pre>
<p>Finally, create a page that loads <code>steal.js</code> and specifies <code>"main"</code> as the main module:</p>
<pre><code><html>
<body>
<script src="./node_modules/steal/steal.js" data-main="main"></script>
</body>
</html>
</code></pre>
<p>It's better if you import just what you need. Instead of loading all of CanJS's core,
we can load just <code>"can/map/map"</code> like:</p>
<pre><code>// main.js
import Map from "can/map/map";
import $ from "jquery";
import template from "./main.stache!";
var data = new Map({message: "Hello World"});
$("body").append(template(data));
</code></pre>
<p>StealJS supports "modlet" module names that end with "/". This means that the above could
also be written like:</p>
<pre><code>// main.js
import Map from "can/map/";
import $ from "jquery";
import template from "./main.stache!";
var data = new Map({message: "Hello World"});
$("body").append(template(data));
</code></pre>
<p>Besides ES6 modules, StealJS supports AMD, and CommonJS. You could also write <code>main.js</code> like:</p>
<pre><code>// main.js
var Map = require("can/map/");
var $ = require("jquery");
var template = require("./main.stache!");
var data = new Map({message: "Hello World"});
$("body").append(template(data));
</code></pre>
<h3>Building to production</h3>
<p>If you are using StealJS and CanJS outside of DoneJS, <a href="http://blog.bitovi.com/using-canjs-2-2-with-stealjs/">this guide</a>
walks through getting a production build.</p>
<h2>Browserify and NPM</h2>
<p>The <a href="https://www.npmjs.com/package/can">can npm package</a> works with browserify. After installing <code>can</code>:</p>
<pre><code>> npm install can --save
</code></pre>
<p>Require the core <code>can</code> modules like:</p>
<pre><code>var can = require("can");
</code></pre>
<p>You can also <code>require</code> specific modules:</p>
<pre><code>var Component = require("can/component/component");
Component.extend({ ... });
</code></pre>
<p>Note that nearly all module names repeat the folder name (ex: <code>can/view/stache/stache</code>).</p>
<h3>Requiring templates</h3>
<p>You can require any CanJS templates using the <a href="https://www.npmjs.com/package/can-compilify">can-compilify</a> Browserify
transform. Complete instructions for installing and using the transform are available on <a href="https://www.npmjs.com/package/can-compilify">npm</a>.</p>
<p>Install can-compilify:</p>
<pre><code>> npm install can --save
</code></pre>
<p>Require a template in your code:</p>
<pre><code>// app.js
var Map = require("can/map/");
var $ = require("jquery");
var template = require("./main.stache");
var data = new Map({message: "Hello World"});
$("body").append(template(data));
</code></pre>
<p>And include the can-compilify transform from the command line:</p>
<pre><code>> browserify -t can-compilify app.js > app.bundle.js
</code></pre>
<p>You could also add can-compilify to your package.json.</p>
<h3>Building templates to production</h3>
<p>Using the can-compilify tranform, your production bundle(s) will include a compiled version of your
templates so no extra setup is required.</p>
<h2>RequireJS from NPM, the Download or Bower</h2>
<p>The Zip Download (on the <a href="http://canjs.com">homepage</a>) contains the following
AMD formatted scripts that can be loaded with RequireJS.</p>
<ul>
<li><code>amd/</code> - CanJS provided as AMD modules</li>
<li><code>amd-dev/</code> - CanJS AMD modules with development messages</li>
</ul>
<p>These files are also available in the <a href="npmjs.com/package/can">can NPM package</a>'s <code>dist</code> folder. Install
the <code>can</code> package like:</p>
<pre><code>> npm install can --save
</code></pre>
<p>You'll find these files in <code>node_modules/can/dist</code>.</p>
<p>These files are also available in the <code>canjs</code> bower package. Install <code>canjs</code> like:</p>
<blockquote>
<p>bower install canjs --save</p>
</blockquote>
<p>You'll find these files in <code>bower_components/can/dist</code>.</p>
<p>The following section contains quick how to load CanJS with RequireJS for different libraries.</p>
<h3>jQuery</h3>
<p>In RequireJS a simple configuration looks like this:</p>
<pre><code><script type="text/javascript" src="require.js"></script>
<script type="text/javascript">
require.config({
paths : {
"jquery" : "http://code.jquery.com/jquery-2.0.3",
"can": "path/to/can/amd"
}
});
require(['can/control', 'can/view/stache'], function(Control, stache) {
// Use Stache and Control
var MyControl = Control.extend({
init: function() {
this.element.html(can.view('path/to/view.stache', this.options));
}
});
});
</script>
</code></pre>
<p>The <code>can</code> module is a shortcut that loads CanJS's core plugins and returns the <code>can</code> namespace:</p>
<pre><code>require(['can'], function(can) {
// Use can.Component, can.view etc.
});
</code></pre>
<h3>Building templates to production</h3>
<p>Bundling templates with the rest of your JavaScript can dramatically reduce the number of requests.</p>
<p>Use the <a href="https://github.com/canjs/can-compile#loading-with-requirejs">can-compile</a> project
to build templates into an AMD module that can be used by <code>r.js</code> to build the app.</p>
<h2>Script tags from NPM, the Download, or Bower</h2>
<p>The Zip Download (on the <a href="http://canjs.com">homepage</a>) contains the following
"global" scripts that can be loaded with just a <code><script></code> tag.</p>
<ul>
<li><code>can.<library>.js</code> (e.g. <code>can.jquery.js</code>) - The core build for a supported library</li>
<li><code>can.<library>.dev.js</code> - A development build logging useful messages for a supported library</li>
<li><code>can.<library>.min.js</code> - The minified core build for a supported library</li>
<li><code>can.<type>.<plugin></code> - Individual builds for each official CanJS plugin</li>
</ul>
<p>These are also available in the <a href="npmjs.com/package/can">can NPM package</a>'s <code>dist</code> folder. Install
the <code>can</code> package like:</p>
<pre><code>> npm install can --save
</code></pre>
<p>You'll find these files in <code>node_modules/can/dist</code>.</p>
<p>These files are also available in the <code>canjs</code> bower package. Install <code>canjs</code> like:</p>
<blockquote>
<p>bower install canjs --save</p>
</blockquote>
<p>You'll find these files in <code>bower_components/can/dist</code>.</p>
<p>The following section contains quick how to load CanJS with a <code><script></code> tag using
these files for each library it supports.</p>
<h3>jQuery</h3>
<p>CanJS supports jQuery in the latest 1.X and 2.X version. Include jQuery before your CanJS jQuery build to get started:</p>
<pre><code><script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js">
</script>
<script src="can.jquery.js"></script>
<script>
$(function() {
// start using CanJS
var Todo = can.Map.extend({
...
});
});
</script>
</code></pre>
<h3>Building templates to production</h3>
<p>CanJS can pre-compile <a href="/docs/can.stache.html">Stache</a> 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 <a href="https://github.com/daffl/can-compile">can-compile</a> Node module. Install with</p>
<pre><code>> npm install can-compile -g
</code></pre>
<p>And in your project root folder run:</p>
<pre><code>> can-compile --out views.production.js
</code></pre>
<p>This will create <code>views.production.js</code> 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.</p>
<h2>Script tags with the download builder</h2>
<p>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 <a href="#section_ScripttagsfromNPM_theDownload_orBower">loading CanJS with script tag using the zip download</a>.</p>
</section>
<script type="text/javascript">
var docObject = {"src":{"path":"docs/can-guides/experiment/setting-up-canjs.md"},"description":"CanJS is packaged in multiple ways so that it can fit into any development workflow. Learn how to setup CanJS in different environments. \n","name":"guides/setup","title":"Setting up CanJS","type":"page","parent":"guides/experiment","order":5,"outline":"2","comment":" ","pathToRoot":"../.."};
</script>
</article>
<footer><p>CanJS is part of <a href="http://donejs.com" target="_blank">DoneJS</a>. Created and maintained by the core <a href="https://donejs.com/About.html#section=section_Team" target="_blank">DoneJS team</a> and <a href="http://bitovi.com" target="_blank">Bitovi</a>. <strong>Currently 3.0.0.</strong></p></footer>
</div>
</div>
</div>
<script>
steal = {
instantiated: {
"bundles/bit-docs-site/static.css!$css" : null
}
};
</script>
<script type='text/javascript' data-main="bit-docs-site/static" src="../static/node_modules/steal/steal.production.js"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</body>
</html>