JavaScript Interface | Bars Language
Bars App is a simple wrapper around Bars that gives you a cleaner interface. Bars App also gives you a nice way to bind DOM interactions/events to functionality within your app.
App contains an accessible reference to Bars for your convenience.
- options
Object
Options including: template, partials, and transforms. - state
Object
An object which is the App State used to render the App View.
Example:
var App = require('bars/app');
/*
* use require('bars/compiled/app')
* if your templates are already compiled.
*/
var options = {
index: mainTemplate,
partials: {
name: template
},
transforms: {
name: func
}
};
var state = {};
var app = new App(options, state);
App state is an object/structure from which the app view is rendered.
This is the method you would call to update the app view. After you manipulate the app state you should call this method to update the view.
- event
String
An event to listen for. - listener
Function
A listener function that gets call onevent
.
Add event listeners to your app.
Example:
app.on('render', function (state) {
console.log('the app view has been rendered');
});
app.on('append', function (target) {
console.log('the app view has been appended to some element', target);
});
- element
Element
The target element to append the app view to.
Use this method to add the app to the page.
Example:
app.appendTo(document.body);
The app.view
is an interactions whose baseTarget is the rendered view from the index template.
Example:
app.view.on('click', '.something', function (evt, target) {
// access data bound to the target
// <div class="something" someData:{{someData}}>
var someData = target.data('someData');
// if you manipulate app.state in anyway you should then call
// app.render();
})
The app.document
is an interactions whose baseTarget is document
.
Example:
app.document.on('ready', function (evt, target) {
console.log('the document is ready');
// if you manipulate app.state in anyway you should then call
// app.render();
})
The app.window
is an interactions whose baseTarget is window
.
Example:
app.window.on('resize', function (evt, target) {
console.log('the window has been resized');
// if you manipulate app.state in anyway you should then call
// app.render();
})
Interactions is a simple user input event router.
- baseTarget
Window|Document|Element
The base target for capturing and routing input events.
Example:
var interactions = new Interactions(baseTarget);
- events
String
A space separated list of events to listen for. - targetSelector
String
A CCS selector to select the target element. - listener
Function
A listener function that gets call onevents
.
Add event listeners to your interactions
.
If a target
is not specified all targets including the baseTarget
and its descendents will trigger the listener
on events
.
Example:
interactions.on('click', '.something', function (evt, $el) {
alert('you clicked on something');
// target maybe the baseTarget or any of its descendents whos selector matches the target selector.
});
interactions.on('click', function (evt, target) {
alert('you clicked on something');
// target maybe the baseTarget or any of its descendents.
});
Bars is a lightweight high performance HTML aware templating engine. Bars emits DOM rather than DOM-strings, this means the DOM state is preserved even if data updates happen. Bars can also emit DOM-strings for backend templating if desired. This way one can use Bars for both static content generation and dynamic web application views.
Example:
var Bars = require('bars');
/*
* use require('bars/compiled')
* if your templates are already compiled.
*/
var bars = new Bars();
- template
String
A Bars template string.
Returns a new Renderer created from the template
.
This method is equivalent to calling bars.build(bars.preCompile(template))
.
NOTE: This method is not available in require('bars/compiled')
variants.
Also see bars-browserify.
Example:
var renderer = bars.compile('<h1>Hello, {{name}}.</h1>');
- template
String
A Bars template string.
Returns a object structure representing the template
.
NOTE: This method is not available in require('bars/compiled')
variants.
Also see bars-browserify.
Example:
var myCompiledTemplate = bars.preCompile('<h1>Hello, {{name}}.</h1>');
- compiledTemplate
Object
A Bars compiled template.
Returns a new Renderer created from the compiledTemplate
.
Example:
var renderer = bars.build(myComiledTemplate);
- name
String
The name of the block helper. - func
Function
The block helper function.
Returns this Bars.
Example:
bars.registerBlock('unless',
function unlessBlock(args, consequent, alternate, context) {
var condition = args[0];
if (condition) {
alternate();
} else {
consequent();
}
}
);
/*
* You can supply a new context to either of the consequent or alternate
* rendering functions by passing in:
* context.newContext(data, props)
*
* data is the new scoped state
* props is an object who's properties are Block Props
* accessible in temples through `@<prop-name>`
*/
/**
* To use the `unless` block in a template
* use this {{#unless <condition> [<context-map>]}} {{else}} {{/unless}}.
*/
- name
String
The name of the partial. - template
String|CompiledTemplate
The template for the partial.
Returns this Bars.
Example:
bars.registerPartial('person', 'I am a partial');
/**
* To use the `person` partial in another
* template use this {{>person [<expression>] [<context-map>]}}.
*/
- name
String
The name of the transform helper. - func
Function
The transform helper function.
Returns this Bars.
Example:
bars.registerTransform('upperCase', function upperCase(a) {
return String(a).toUpperCase();
});
/**
* To use the `upperCase` transform in a
* template use this {{@upperCase(<expression>)}}.
*/
A renderer is an object that can render/update a DOM view.
- compiledTemplate
Object
An object structure containing a pattern for rendering.
- data
Object
Object context for rendering update. - options
Object
Options for indentation.- tabs
Boolean
Use tabs for indentation. - indent
Number
Number of spaces to indent with if not using tabs.
- tabs
Returns DOM-string text.
Renders DOM-string text with data
.
Example:
var renderedText = renderer.text({name: 'Bob'}, {tabs: false, indent: 2});
- data
Object
Object context for rendering update.
Returns this Renderer
Updates/renders the view with the new data
.
Example:
renderer.update({name: 'Bob'});
- element:
Element
The target element to append the app view to.
Returns this Renderer.
NOTE: if you do not call renderer.update(data)
, the view will be empty. You can call renderer.update(data)
before or after calling renderer.appendTo(el)
.
Example:
renderer.appendTo(document.body);