Tiny single purpose view engine, help componentize your javascript. Chemical will help you make super performant UI apps.
No abstractions, limit sugar. Keep your self close to the browser, but create compostable, and reusable UIs.
Compatible with any template engine that accepts a model, and returns HTML. Use precompiled templates for blazing performance.
Compatible with AMD, CJS, or can be found at c$ if no module loader is used.
Compatible with Google Closure Compiler
npm install psychic --save-dev
Chemical tries to be ultra fast by limiting the actual representation from the declarative api, to rendered DOM elements.
We generate HTML based on your outer Control's components. Events are wired to a single rendered container, utilizing a state machine like Redux - we can achieve some crazy performance.
Extend any component at runtime using our declarative API, or leverage defining prototypes for parse time performance improvements!
Chemical helps you manage events. We rely on native event implementations, but provide a small amount of sugar to abstract on them for you. Including condensing events to a single rendered root, detaching and reattaching at render time!
Chemical has a full stack of lifeCycle events! You can listen for events that range from
afterRender, to
beforeUpdate.
It's amazing we fit all of this into less than 2kb of compressed space! For an additional 1kb you can bring our state machine with! That's only 3kb gzipped to get similar functionality as ReactJS + Redux!
And yes, it is universal JS compatible, and views can be rendered on the server.
Generic Hello World
var Component = require'psychic';var component =return datamessage;message: 'Hello World';component;
Compose components
var Component = require'psychic';var component =components:return 'Hello World';;component;
Another way to do hello world, we provide a default template
var Component = require'psychic';var component =innerHTML: 'Hello World';component;
We render components into this innerHTML for you. We collapse the representation of the DOM, and exploit the super fast innerHTML implementations developed over the years.
var Component = require'psychic';var component =return '<chrome>' + datainnerHTML + '</chrome>';components:innerHTML: 'hello world';component;
Chemical currently has two core blocks to build with,
control
and
component. A
control
is used when you expect a
component
to render. It adds a bit of extra decoration to a base component. The API is small and consists of these calls:
component
control
(in addition to the above)
API EXAMPLES
var Component = require('psychic').component;
// mix// mixes data into// the component rootcomponent;// component hash// provides a quick and easy way to// access named components.//// components are only hashed on render// and not at contruction timevar component =name: 'myComponent';component;component$myComponent;// guid//// instanced components will be provided with a guid for eventing// this guid changes on page reload, but persists while the application// is loadedcomponentguid// listen//// listen for browser events. Events are wired against outer container// nodes. Only the root rendered control will be listening, and delegating// to target child nodes when appropriate.//// listeners are tore down before every re-render. This is to encourage the// developer to think sparingly about event usage within the view librarycomponent;;// listeners should be inserted using afterRender.var component =this;;// listeners attach to the parent nodevar component =components:var component =// the root rendered parent will own the click// the click will be delegated to this handlerthis;;;/// dispatch////// dispatches event that listeners are waiting to receive/// dispatch is throttled to 1 call per 80ms. Plan accordingly. The best use of this/// is to request the state machine to draw the view./// this does not influence native event dispatches, only those sent you .dispatch///component; ;component;/// destroy////// Tears down the component, child components, event handlers, but does not/// destroy the instance.component;
var Control = require'psychic'control;`/// update////// updates the component's internal rendering/// if passed data, will also call .mix, before re-rendering.////// if .update is called on a parent that node will be replaced/// in the DOM.// with no data, just ask for a re-render, and reinsertion// into the DOM if has a represented node.control.update();// send data to use before rendering and DOM swapcontrol.update({foo: 'bar'});/// renderInto////// Renders a control into a DOM target.control.renderInto(document.body);/// render////// Renders the control with the currently bound data set./// Great for rendering on the server./// We also provide a view engine that does this lifting for you.control.render();
Components are mainly composed in Chemical using the declarative syntax. This allows natural composition that most developers are familiar with in view libraries.
// the base default declarative syntax for a control objectvar definition =name: 'myComponent' // optional name for reference hashingtag: 'div' // root node type, default is DIVattributes: // any DOM attribute to append to root node'class': 'red''data-foo': 'bar'{} // LifeCycle hookscomponents: // composition block, add components for reuse// the template provides the innerHTML structure of the component// If a components block is defined on this component, data.innerHTML// will be populated, so you can template chrome around your componentsreturn 'Hello World'innerHTML: '' // a simple string that is returned to the template, the default template renders this{} // Methods you want to travel with the component;// instance a new component by passing it// the definitionvar control = definition;
Sometimes we want to extend components for reusability. We also want to get some performance gains when defining new components. Chemical leans heavily on prototypal inheritance to get some nice performance from our paradigm.
var Control = require'psychic'control;// do some custom stuff hereControlcallthis data;;
Chemical provides component LifeCycle events.
wip
Here is a list of all the LifeCycle events that can be defined
beforeUpdate
/
afterUpdate
beforeRender
/
afterRender
beforeDestroy
/
afterDestroy
beforeDispatch
/
afterDispatch
Chemical allows you to render components on the server, and then mount them client side. It requires little to no effort on your part. Simply give your components a
name.
var Control = require('psychic').control;module.exports = new Control({name: 'myMountableComponent',components: [new Control({name: 'myButton',template: <button>Click Me</button>,handlers: {'click': function(e) {alert('I was clicked!');}}})]});
It is preferential to use a State Store to maintain the state of your component. Utilizing the
FLUX
or one way data flow model, we can increase reliability and performance of our UI.