UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

893 lines (625 loc) 26.9 kB
<!DOCTYPE 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> &nbsp; </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&#x27;s mission, why it matters, and how we&#x27;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&#x27;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&#x27;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&#x27;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&#x27;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&#x27;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&#x27;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>&lt;script&gt;</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>&lt;html&gt; &lt;head&gt; &lt;title&gt;CanJS Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;http://canjs.com/release/latest/can.jquery.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;http://canjs.com/release/latest/can.map.define.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;http://canjs.com/release/latest/can.stache.js&quot;&gt;&lt;/script&gt; &lt;script type='text/stache' id='app'&gt; &lt;hello-world/&gt; &lt;/script&gt; &lt;script type=&quot;text/javascript&quot;&gt; can.Component.extend({ tag: 'hello-world', template: can.stache(&quot;&lt;h1&gt;{{message}}&lt;/h1&gt;&quot;), viewModel: { message: &quot;Hi there!&quot; } }); $(&quot;body&quot;).append(can.view(&quot;app&quot;,{})); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </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>&lt;script src=&quot;http://zeptojs.com/zepto.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;http://canjs.com/release/latest/can.zepto.js&quot;&gt;&lt;/script&gt; </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>&gt; npm install can --save &gt; npm install steal --save &gt; npm install jquery --save </code></pre> <p>Next, create a <code>main.stache</code> template for your app:</p> <pre><code>&lt;!-- main.stache --&gt; &lt;h1&gt;{{message}}&lt;/h1&gt; </code></pre> <p>Next, create a <code>main</code> module for your application. Import CanJS's core, jQuery, and your template to say &quot;Hello World&quot;:</p> <pre><code>// main.js import can from &quot;can&quot;; import $ from &quot;jquery&quot;; import template from &quot;./main.stache!&quot;; var data = new can.Map({message: &quot;Hello World&quot;}); $(&quot;body&quot;).append(template(data)); </code></pre> <p>Finally, create a page that loads <code>steal.js</code> and specifies <code>&quot;main&quot;</code> as the main module:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;script src=&quot;./node_modules/steal/steal.js&quot; data-main=&quot;main&quot;&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </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>&quot;can/map/map&quot;</code> like:</p> <pre><code>// main.js import Map from &quot;can/map/map&quot;; import $ from &quot;jquery&quot;; import template from &quot;./main.stache!&quot;; var data = new Map({message: &quot;Hello World&quot;}); $(&quot;body&quot;).append(template(data)); </code></pre> <p>StealJS supports &quot;modlet&quot; module names that end with &quot;/&quot;. This means that the above could also be written like:</p> <pre><code>// main.js import Map from &quot;can/map/&quot;; import $ from &quot;jquery&quot;; import template from &quot;./main.stache!&quot;; var data = new Map({message: &quot;Hello World&quot;}); $(&quot;body&quot;).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(&quot;can/map/&quot;); var $ = require(&quot;jquery&quot;); var template = require(&quot;./main.stache!&quot;); var data = new Map({message: &quot;Hello World&quot;}); $(&quot;body&quot;).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>&gt; npm install can --save </code></pre> <p>Require the core <code>can</code> modules like:</p> <pre><code>var can = require(&quot;can&quot;); </code></pre> <p>You can also <code>require</code> specific modules:</p> <pre><code>var Component = require(&quot;can/component/component&quot;); 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>&gt; npm install can --save </code></pre> <p>Require a template in your code:</p> <pre><code>// app.js var Map = require(&quot;can/map/&quot;); var $ = require(&quot;jquery&quot;); var template = require(&quot;./main.stache&quot;); var data = new Map({message: &quot;Hello World&quot;}); $(&quot;body&quot;).append(template(data)); </code></pre> <p>And include the can-compilify transform from the command line:</p> <pre><code>&gt; browserify -t can-compilify app.js &gt; 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>&gt; 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>&lt;script type=&quot;text/javascript&quot; src=&quot;require.js&quot;&gt;&lt;/script&gt; &lt;script type=&quot;text/javascript&quot;&gt; require.config({ paths : { &quot;jquery&quot; : &quot;http://code.jquery.com/jquery-2.0.3&quot;, &quot;can&quot;: &quot;path/to/can/amd&quot; } }); 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)); } }); }); &lt;/script&gt; </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 &quot;global&quot; scripts that can be loaded with just a <code>&lt;script&gt;</code> tag.</p> <ul> <li><code>can.&lt;library&gt;.js</code> (e.g. <code>can.jquery.js</code>) - The core build for a supported library</li> <li><code>can.&lt;library&gt;.dev.js</code> - A development build logging useful messages for a supported library</li> <li><code>can.&lt;library&gt;.min.js</code> - The minified core build for a supported library</li> <li><code>can.&lt;type&gt;.&lt;plugin&gt;</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>&gt; 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>&lt;script&gt;</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>&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js&quot;&gt; &lt;/script&gt; &lt;script src=&quot;can.jquery.js&quot;&gt;&lt;/script&gt; &lt;script&gt; $(function() { // start using CanJS var Todo = can.Map.extend({ ... }); }); &lt;/script&gt; </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>&gt; npm install can-compile -g </code></pre> <p>And in your project root folder run:</p> <pre><code>&gt; 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>