Creating a plugin
DoneJS doesn't just make it easy to build high performance, real-time web and mobile applications. It can also be used to create reusable plugins that can be shared across different applications. In this guide we will create a reusable number input widget using Bootstrap styles.
We will create the project on GitHub, initialize the repository as a new DoneJS plugin and then set up continuous integration with Travis CI. After running development mode we will implement the component functionality and tests and submit it as a pull request to the repository. Finally we will make a build and publish the plugin to npm as well as look how to use the published module in other projects.
You can find the code in the donejs-number-input repository. The final result looks like this:
JS Bin on jsbin.com
Creating the project on GitHub
We will use GitHub to host the code for the project which makes it easy for others to contribute and to automatically run the tests in continuous integration which we will enable later.
If you don't have an account yet, go to GitHub to sign up and follow the help on how to set it up for the command-line
git. Once completed, you can create a new repository from your dashboard.
Calling the repository
<username>-number-inputand initializing it empty (without any of the default files) looks like this:
After creating the repository, we can clone it into a new folder:
Initializing the plugin
To initialize a new plugin you will need DoneJS version 0.7.0+ installed globally. To check your DoneJS version run
To install DoneJS or to get the latest version run:
<username>-number-inputfolder we can now initialize a new plugin like this:
The plugin generator will ask several question that should be answered as follows:
Once all done, the final prompt looks similar to this:
Now the generator will initialize the default plugin layout and install all its dependencies.
Setting up Travis CI
When the installation has completed, we can make sure everything got set up properly by running:
This will open a Firefox browser, run two tests and output the result on the console.
This command can also be used to automatically run the tests on a continuous integration server. There are many open source CI servers, the most popular being Jenkins, and many hosted solutions like Travis CI.
We will use Travis CI as our hosted solution because it is free for open source projects. It works with your GitHub account which it will use to sign up. Once signed in, go to
Accounts(in the dropdown under you name) to enable the
You may have to click the "Sync account" button for the repository to show up. Now, every time we push to GitHub the tests will run automatically. We can do so with our initial commit:
If you now go
https://travis-ci.org/<your-username>/<username>-number-input/buildsyou will see the build running and eventually turn green (which will update the badge that got added in the
Like a DoneJS application, a DoneJS plugin provides a development mode that starts a server and enables live-reload by running:
The server will run at
http://localhost:8080. You can view the main test page at localhost:8080/src/test/test.html. Any changes to the test file or module will re-run the tests right away thanks to hot-module-swapping.
Creating the component
A plugin can contain anything from shared utility functions to model- or component collections. Just like in a DoneJS application it is possible to add components and models. In our case we want to create a new component which we can do like this:
This creates a complete component using the
<username>-number-inputtag with tests and documentation. Because the module name is the same as the plugin name (
<username>-number-input), the generator will put the component files directly in the
src/folder (instead of a subfolder). Confirm the default tag name by pressing enter. Confirm the prompts to overwrite the existing files by typing
Yand pressing enter. The initialized component can now be viewed at
http://localhost:8080/src/<username>-number-input.html. The component tests are available at localhost:8080/src/test.html.
Creating and testing the view-model
Our number input view-model should provide the following functionality:
We can use the define plugin to define a
maxvalue and a setter for the
valueto make sure that it always is within those constraints. We will also add an
decrementmethod that will modify the value by 1. The component view-model (in
src/<username>-number-input.js) then looks like this:
To test this functionality, we can change the tests in
src/<username>-number-input-test.jsto look like this:
You can run all tests either by going to localhost:8080/src/test/test.html in the browser or via
Adding the template
In the template we will use Bootstrap and can-view-import, so first install them as dependencies:
Then we can update
src/<username>-number-input.stacheto look like this:
This template first imports the Bootstrap LESS. Then we create a button group with a
-button on the left, a number input in the middle and a
+button on the right. When the buttons are clicked the
decrementview-model methods are being called. The value of the input field is two-way bound with the
valueproperty of the view-model. When the value is either
+buttons will be disabled.
Publishing the plugin
Making a pull request
Although we are working on the plugin by ourselves for now, GitHub pull requests are a great way to keep track of our progress and to make sure that all tests are passing. In the plugin folder we can run:
And then create a new pull request by going to
https://github.com/<your-username>/<username>-number-inputwhich will now show an option like this:
Once you created the pull request, you will see a
Some checks haven’t completed yetmessage that will eventually turn green:
Now you can click the "Merge pull request" button. Then in the console, checkout the master branch and pull down the latest changes with:
Making a build
Now that we implemented the number input functionality and have all tests passing we can make a build of our plugin that is usable standalone in the Browser, with an AMD module loader like RequireJS or as a CommonJS module which works e.g. with Browserify.
Will create a
dist/folder with the
commonjsversion of our plugin.
Publishing to npm
npm is the best way to share modules and make them easily installable without having to manage dependencies manually. To be able to publish your own modules, create a new account and then run
Semantic versioning is a great way to communicate new features and breaking changes. The generated plugin already comes with the release scripts to publish new versions according to the
major.minor.patchschema. In our case to publish an initial version
0.1.0we can run
0.1.0of our plugin is available on npm.
Usage in other projects
In another DoneJS application we can now install the plugin with
Then import it in a template and load it with:
Show it off
Once you published your plugin, let the world know about it. Tweet @donejs and post it in the Bitovi forums and on Slack (in the #donejs channel). Those are also great places to get quick help with any questions.