A more convenient way to get the latest version of CanJS and easily stay is the Bower package manager.
If you haven't yet, initialize a bower.json in your project folder by running
bower init
After that we need to add jQuery and CanJS as dependencies:
bower install jquery canjs --save
And the following index.html in the same folder, that also loads the Pushstate and can.Map.backup plugins:
A fairly common setup is using CanJS with RequireJS and Bower as the package manager which is described in the following section. For a working example have a look at the RequireJS + CanJS TodoMVC example.
If you haven't yet, initialize a bower.json in your project folder by running
bower init
After that we need to add jQuery, RequireJS and CanJS as dependencies:
bower install jquery requirejs canjs --save
And initialize RequireJS on our HTML page and point the root module to js/app:
require(['can/control', 'can/view/mustache'], function(Control, can) {
// Use Mustache and Control
return Control.extend({
init: function() {
this.element.html(can.view('views/index.mustache', {
message: 'CanJS'
}));
}
});
});
In view/index.mustache:
<h1>Hello {{message}}!</h1>
You are set up and good to go. Follow up in the Using CanJS in production section on how to pre-compile your views and make a build using the RequireJS optimizer.
jQuery from a CDN
The easiest way to get started with CanJS is to create the following
index.html
which loads everything from a CDN:Bower + Grunt
A more convenient way to get the latest version of CanJS and easily stay is the Bower package manager. If you haven't yet, initialize a
bower.json
in your project folder by runningAfter that we need to add jQuery and CanJS as dependencies:
And the following
index.html
in the same folder, that also loads the Pushstate and can.Map.backup plugins:In
js/app.js
:An easy way to get this setup production ready is creating an
index.production.html
that references the production files:With Grunt as a build tool your
Gruntfile.js
to createproduction.js
can look like this:Bower + RequireJS
A fairly common setup is using CanJS with RequireJS and Bower as the package manager which is described in the following section. For a working example have a look at the RequireJS + CanJS TodoMVC example.
If you haven't yet, initialize a
bower.json
in your project folder by runningAfter that we need to add jQuery, RequireJS and CanJS as dependencies:
And initialize RequireJS on our HTML page and point the root module to
js/app
:In
js/app.js
:In
js/my-control.js
:In
view/index.mustache
:You are set up and good to go. Follow up in the Using CanJS in production section on how to pre-compile your views and make a build using the RequireJS optimizer.