knockout-jsx
JSX is not coupled to React. This little library will enable its use with Knockout instead.
Installation
Install knockout-jsx through npm.
npm install --save knockout-jsx
Once installed, knockout-jsx supports several types of imports.
ES6
;
CommonJS
const registerJSXComponent = ;// Or...const knockoutJSX = ;
UMD
Referencing either dist/knockout-jsx.js
or dist/knockout-jsx.min.js
will add the API
methods to ko.jsx
.
<!-- Or... -->
Usage
Build Step
JSX must be transpiled to DOM API methods prior to using this libary. nativejsx is recommended. See the build tools section of the nativejsx readme for instructions on use with Webpack, Gulp, Grunt, or a shell command.
Components
This library uses
Knockout Components
and registers components with ko.components.register()
.
import registerJSXComponent from 'knockout-jsx'; { const self = this; selfcounter = ko; selfcounterText = ko; self self;} const template = <div> <div ="text: counterText"></div> <button ="button" ="click: incrementCounter"></button> </div>; ;
Then, in the page markup:
Here's a counter! <!-- component will be rendered here by Knockout -->
API
registerJSXComponent(name, component)
Registers a given component with Knockout. This is just a wrapper for ko.components.register
.
Arguments
name
: The name of the component. This is the HTML element where a component will be rendered. For example, if a component with thename
'counter' is passed to this method, the component will be rendered wherever a<counter></counter>
tag appears in the page markup. The value ofname
must be all lowercase.component
: The component object. This must have the following properties:viewModel
: The view model definition for the component.jsxTemplate
: The JSX template object to use for the component. A build tool must convert the JSX markup to DOM API methods prior to callingregisterJSXComponent
. As such, this should actually be an Element.
Testing
Run npm run test
to start the Karma
test runner with PhantomJS.
To just run the tests once, run npm run test-once
.
License
Licensed under the MIT License.