Builder
Build chain for client-side MVC views.
Render, convert to DOM elements, and bind jQuery plugins in one fell swoop.
Getting Started
Download any of the versions below. A definition of each preset is presented later in the documentation.
In your web page:
<script src="jquery.js"></script>
<script src="dist/Builder.min.js"></script>
<script>
Builder.set('template engine', jade.render);
Builder.addPlugin('datepicker');
Builder.addPlugin('timepicker');
var template = [
'#main',
' input.datepicker',
' input.timepicker'
].join('\n'),
$content = Builder(template);
$content;
$content.find('.datepicker');
</script>
Documentation
Builder is run as a standalone function that you can configure as you please.
Builder(tmpl, data);
As mentioned in the documentation above, Builder runs through 4 steps.
Each of these steps has access to the original data passed into Builder via this
.
This is also where you can/should modify data in before
.
this;
Builder.before(fn);
Builder.template(tmpl, data);
Builder.domify(content);
Builder.after(fn);
Lastly, we can update our template engine
(e.g. jade, ejs, mustache) and dom engine
(e.g. jQuery, Mason.js, innerHTML).
The template engine
takes views and interprets the template along with data to output content.
template engine
recieves (<template string>, <data object>)
as parameters and is expected to return a string
synchronously.
The dom engine
takes strings and converts them into HTML elements, jQuery collections, or similar.
Builder.set(name, val);
Builder.set(object);
Presets
Vanilla
Builder with no adjustments.
jQuery
Sets dom engine
to jQuery and adds
Builder.addPlugin(plugin);
Builder.addPlugin({'plugin': plugin, 'selector': selector});
keys
Adds a before
method which will look up templates by a name rather than accepting the template directly through Builder.
Additionally, we add an addView
method which allows you to add new views to look up.
Builder.addView(name, tmpl);
require
Defines Builder
within define
function of require.js
Examples
jQuery
See Getting Started.
keys
Builder.addView('sample', '<div>Sample!!</div>');
Builder('sample');
require
require(['Builder'], function (Builder) {
var $content = Builder('<div>some content</div>');
});
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Also, please don't edit files in the "dist" or "stage" subdirectories as they are generated via grunt. You'll find source code in the "lib" subdirectory!
PhantomJS
While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html
unit test file(s) in actual browsers.
See the Why does grunt complain that PhantomJS isn't installed? guide in the Grunt FAQ for help with installing or troubleshooting PhantomJS.
License
Copyright (c) 2013 Ensighten
Licensed under the MIT license.