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
ObjectOptions including: template, partials, and transforms. - state
ObjectAn 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
StringAn event to listen for. - listener
FunctionA 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
ElementThe 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|ElementThe base target for capturing and routing input events.
Example:
var interactions = new Interactions(baseTarget);- events
StringA space separated list of events to listen for. - targetSelector
StringA CCS selector to select the target element. - listener
FunctionA 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
StringA 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
StringA 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
ObjectA Bars compiled template.
Returns a new Renderer created from the compiledTemplate.
Example:
var renderer = bars.build(myComiledTemplate);- name
StringThe name of the block helper. - func
FunctionThe 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
StringThe name of the partial. - template
String|CompiledTemplateThe 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
StringThe name of the transform helper. - func
FunctionThe 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
ObjectAn object structure containing a pattern for rendering.
- data
ObjectObject context for rendering update. - options
ObjectOptions for indentation.- tabs
BooleanUse tabs for indentation. - indent
NumberNumber 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
ObjectObject context for rendering update.
Returns this Renderer
Updates/renders the view with the new data.
Example:
renderer.update({name: 'Bob'});- element:
ElementThe 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);