can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
297 lines (245 loc) • 12.6 kB
HTML
<!--####################################################################
THIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN
INSTEAD CHANGE:
source: [object Object]
@page Plugins
######################################################################## -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>CanJS - Developing Plugins</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>
</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="Plugins.html">Developing Plugins</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>Developing Plugins</h1>
<div>page</div>
</div>
<section class="description">
</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/plugins.md">source</a></li>
<!-- <li><a href="#">download</a></li> -->
<!-- <li><a href="#">tests</a></li> -->
</ul>
</section>
<section class="body">
<p>In this guide you'll learn how to build a plugin for CanJS. As is standard with programming tutorials,
we’ll build something that says "Hello World".</p>
<h2>Setup</h2>
<p>We'll be using <a href="http://yeoman.io">Yeoman</a> and a generator built to scaffold CanJS plugins. If you don't want to use Yeoman, you will have to create the files mentioned in this guide yourself. The <a href="https://github.com/ccummings/canjs-hello-world">canjs-hello-world</a> repository is where you can grok all of the files the Yeoman generator creates.</p>
<h3>1) Install Yeoman</h3>
<p>The first thing you need to do is install Yeoman and <a href="https://github.com/ccummings/generator-canjs-plugin"><code>generator-canjs-plugin</code></a> which will allow you to quickly scaffold a CanJS plugin.</p>
<p>Install both by running the following on the command line:</p>
<pre><code>npm install -g yo generator-canjs-plugin
</code></pre>
<p><em>For help installing or using Yeoman, there is the <a href="http://yeoman.io/gettingstarted.html">Yeoman getting started guide</a></em></p>
<h3>2) Run the generator</h3>
<p>Next create a directory for your plugin and <code>cd</code> into it:</p>
<pre><code>mkdir canjs-hello-world && cd canjs-hello-world
</code></pre>
<p>Then run the generator:</p>
<pre><code>yo canjs-plugin
</code></pre>
<p>After answering a series of questions about your plugin the generator will create the files and install all of the dependencies you need to develop, test and publish your CanJS plugin.</p>
<h2>Develop</h2>
<p>Now that you have a scaffold in place, you'll start building your plugin.</p>
<h3>Modify the Source</h3>
<p>A single source file will be placed in the <code>src</code> directory. This is where you will add your plugin code. Here's what your plugin should look like:</p>
<pre><code>can.hello = function() {
return 'Hello World';
};
</code></pre>
<p><strong>Notice that the file includes bootstrapping that allows it to work with AMD loaders, Steal or stand alone with no dependency management tool.</strong></p>
<h3>Write tests</h3>
<p>A <a href="https://qunitjs.com/">QUnit</a> test file and runner are placed in the <code>test</code> folder. The <code>*_tests.js</code> file is where you will write tests for your plugin. Modify the <code>Hello World</code> test so it looks like this:</p>
<pre><code>test('Hello World', function() {
equal(can.hello(), 'Hello World', 'Works!');
});
</code></pre>
<p>You can run these tests by opening <code>test/qunit.html</code> in a browser or by running <code>grunt test</code> on the command line. Do this now and make sure the test passes.</p>
<h3>Create examples</h3>
<p>Examples for AMD, Steal and stand alone are placed in the <code>example</code> folder. Modify the <code>index.html</code> file in each folder to show people how to use your plugin.</p>
<p>In each of the 3 <code>index.html</code> files replace the line <code>//Demo JS goes here</code> with:</p>
<pre><code>console.log(can.hello());
</code></pre>
<p>Open these files in a browser and look at the console to see "Hello World" being logged.</p>
<h3>Create Documentation</h3>
<p>It's highly recommended that you create documentation for your plugin. You can use your plugin repo's wiki or generate a website for it using <a href="https://pages.github.com/">GitHub pages</a>.</p>
<h2>Grunt work flow</h2>
<p>Now that you have developed a plugin, tests and examples, it’s time to build the plugin using Grunt. The following commands are available:</p>
<p>Run jsbeautifier and JSHint:</p>
<pre><code>grunt quality
</code></pre>
<p>Run tests:</p>
<pre><code>grunt test
</code></pre>
<p>Run tests and places distributable files in the <code>dist</code> folder:</p>
<pre><code>grunt build
</code></pre>
<h2>Distribute your plugin with Bower</h2>
<p>The easiest way to distribute your plugin is via <a href="http://bower.io">Bower</a> In order to create a bower package you'll need:</p>
<ul>
<li>A valid manifest file (<a href="http://bower.io/#defining-a-package">bower.json</a>) in the root of the project</li>
<li>All code available at a Git endpoint (hosted at GitHub or BitBucket for example)</li>
<li>To register your plugin with Bower</li>
</ul>
<h3>The manifest</h3>
<p>A valid <code>bower.json</code> file is created by the generator. If you are coding your plugin from scratch run <code>bower init</code> and answer the series of prompts to generate your own.</p>
<p><strong>Be sure to include relevant keywords in your manifest. For CanJS plugins use <code>can</code> and any other keywords that are relevant. For can.Components add a keyword of <code>can-components</code>.</strong></p>
<h3>Git endpoint</h3>
<p>Each version of your plugin must have a <a href="http://semver.org/">semantic version number</a> and a corresponding Git tag with the same version. You create a Git tag like this:</p>
<pre><code>$ git tag -a vX.Y.Z -m 'vX.Y.Z'
</code></pre>
<h3>Register with Bower</h3>
<p>Now that you have a valid manifest a Git endpoint and Git tag you are ready to register your plugin. To do this use the <code>bower register</code> command:</p>
<pre><code>$ bower register [plugin-name] [git-endpoint]
</code></pre>
<p><strong>[git-endpoint] is the URL to your repository, typically starting with <code>git://</code>.</strong></p>
<p>Your plugin is now available via the <a href="http://bower.io/search">bower registry</a>!</p>
<h2>Wrapping up</h2>
<p>That's all there is to it. In this guide you developed a CanJS plugin complete with tests and examples and learned how to distribute it via bower.</p>
<p>If you've created a CanJS plugin we'd love to hear about it over on <a href="http://bithub.com">BitHub</a> <a href="http://webchat.freenode.net/?channels=canjs">IRC</a> or our <a href="http://forums.donejs.com/c/canjs">forums</a>.</p>
</section>
<script type="text/javascript">
var docObject = {"src":{"path":"docs/can-guides/plugins.md"},"description":"\n","name":"Plugins","title":"Developing Plugins","type":"page","parent":"contributing","order":0,"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>