Skip to content

Commit

Permalink
Removed global Handlebars dependency, instead the Handlebars object i…
Browse files Browse the repository at this point in the history
…s referenced in the register() method
  • Loading branch information
badsyntax committed May 25, 2013
1 parent 8d15b4f commit eaa42be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
26 changes: 13 additions & 13 deletions README.md
@@ -1,15 +1,15 @@
# handlebars-form-helpers # handlebars-form-helpers


[![Build Status](https://travis-ci.org/badsyntax/handlebars-form-helpers.png?branch=master)](https://travis-ci.org/badsyntax/handlebars-form-helpers) [![Build Status](https://travis-ci.org/badsyntax/handlebars-form-helpers.png?branch=master)](https://travis-ci.org/badsyntax/handlebars-form-helpers)
[![Dependency Status](https://gemnasium.com/badsyntax/handlebars-form-helpers.png)](https://gemnasium.com/badsyntax/handlebars-form-helpers) [![Dependency Status](https://gemnasium.com/badsyntax/handlebars-form-helpers.png)](https://gemnasium.com/badsyntax/handlebars-form-helpers)


This library provides handlebars helpers that help with building forms and handling validation errors. This library provides handlebars helpers that help with building forms and handling validation errors.


## Installation ## Installation


### Browser ### Browser


Either [download](https://raw.github.com/badsyntax/handlebars-form-helpers/master/dist/handlebars.form-helpers.min.js) the Either [download](https://raw.github.com/badsyntax/handlebars-form-helpers/master/dist/handlebars.form-helpers.min.js) the
script, or install with [bower](http://bower.io/): `bower install handlebars-form-helpers` script, or install with [bower](http://bower.io/): `bower install handlebars-form-helpers`


Load the scripts into your page, ensure you load the form helpers after handlebars: Load the scripts into your page, ensure you load the form helpers after handlebars:
Expand All @@ -20,7 +20,7 @@ Load the scripts into your page, ensure you load the form helpers after handleba
Then register the helpers: Then register the helpers:


```javascript ```javascript
Handlebars.formHelpers.register(); HandlebarsFormHelpers.register(Handlebars);
``` ```


### Node/CommonJS ### Node/CommonJS
Expand All @@ -32,29 +32,29 @@ dependency), as well as register the helpers:


```javascript ```javascript
var hbs = require('hbs'); var hbs = require('hbs');
require('handlebars-form-helpers')(hbs.handlebars).register(); require('handlebars-form-helpers').register(hbs.handlebars);
``` ```


### AMD ### AMD


As with the CommonJS module, you need to initiate the AMD module manually, as well as register the helpers: As with the CommonJS module, you need to initiate the AMD module manually, as well as register the helpers:


```javascript ```javascript
define(['handlebars', 'handlebars-form-helpers'], function(handlebars, handlebarsHelpersInit) { define(['handlebars', 'handlebars-form-helpers'], function(handlebars, handlebarsHelpers) {
handlebarsHelpersInit(handlebars).register()'' handlebarsHelpers.register(handlebars);
// ..etc // ..etc
}); });
``` ```


## Usage ## Usage


Most of the helpers can be used inline, for example: Most of the helpers can be used inline, for example:


``` ```
{{label "name" "Please enter your name"}} {{label "name" "Please enter your name"}}
``` ```


The only block helpers are the form and field_errors helpers: The only block helpers are the form and field_errors helpers:


``` ```
{{#form "/post" class="form"}}{{/form}} {{#form "/post" class="form"}}{{/form}}
Expand All @@ -63,16 +63,16 @@ The only block helpers are the form and field_errors helpers:
{{/field_errors}}` {{/field_errors}}`
``` ```


By default the helpers are registered without a namespace. This gives you nice and friendly helper names. If you need to By default the helpers are registered without a namespace. This gives you nice and friendly helper names. If you need to
change the helpers namespace (because helper names are conflicting with template data vars), then you can use change the helpers namespace (because helper names are conflicting with template data vars), then you can use
the `namespace()` public API method to set a custom namespace, before registering the helpers, for example: the `namespace()` public API method to set a custom namespace, before registering the helpers, for example:


```javascript ```javascript
Handlebars.formHelpers.namespace('myform'); // set the namespace before registering HandlebarsFormHelpers.namespace('myform'); // set the namespace before registering
Handlebars.formHelpers.register(); HandlebarsFormHelpers.register(Handlebars);
``` ```


Now the helpers are created with that namespace, for example: Now the helpers are created with that namespace, for example:


``` ```
{{myform-label "name" "Please enter your name"}} {{myform-label "name" "Please enter your name"}}
Expand Down
2 changes: 1 addition & 1 deletion examples/form_validation.html
Expand Up @@ -185,7 +185,7 @@
compile: function() { compile: function() {


// Register the form helpers // Register the form helpers
Handlebars.formHelpers.register(); HandlebarsFormHelpers.register(Handlebars);


// Pre-compile the template // Pre-compile the template
this.source = $('#form-template').html(); this.source = $('#form-template').html();
Expand Down
20 changes: 8 additions & 12 deletions spec/handlebars.form-helpers.spec.js
@@ -1,31 +1,27 @@
describe('Handlebars form helpers', function() { describe('Handlebars form helpers', function() {


it('Depends on Handlebars', function() {
expect(typeof Handlebars).not.toBe('undefined');
});

describe('Public API', function() { describe('Public API', function() {


it('Has \'register\' and \'namespace\' methods', function() { it('Has \'register\' and \'namespace\' methods', function() {
expect(typeof Handlebars.formHelpers).toBe('object'); expect(typeof HandlebarsFormHelpers).toBe('object');
expect(typeof Handlebars.formHelpers.register).toBe('function'); expect(typeof HandlebarsFormHelpers.register).toBe('function');
expect(typeof Handlebars.formHelpers.namespace).toBe('function'); expect(typeof HandlebarsFormHelpers.namespace).toBe('function');
}); });


it('Sets or gets the namespace', function() { it('Sets or gets the namespace', function() {
Handlebars.formHelpers.namespace('test'); HandlebarsFormHelpers.namespace('test');
expect(Handlebars.formHelpers.namespace()).toBe('test-'); expect(HandlebarsFormHelpers.namespace()).toBe('test-');
}); });


it('Registers the form helpers with the namespace', function() { it('Registers the form helpers with the namespace', function() {


// Reset the namespace after running this test // Reset the namespace after running this test
this.after(function() { this.after(function() {
Handlebars.formHelpers.namespace(''); HandlebarsFormHelpers.namespace('');
Handlebars.formHelpers.register(); HandlebarsFormHelpers.register(Handlebars);
}); });


Handlebars.formHelpers.register(); HandlebarsFormHelpers.register(Handlebars);
expect(typeof Handlebars.helpers['test-form']).not.toBe('undefined'); expect(typeof Handlebars.helpers['test-form']).not.toBe('undefined');
}); });
}); });
Expand Down
27 changes: 17 additions & 10 deletions src/handlebars.form-helpers.js
Expand Up @@ -4,27 +4,30 @@
* Copyright (c) 2013 Richard Willis; Licensed MIT * Copyright (c) 2013 Richard Willis; Licensed MIT
*/ */


(function (factory) { (function (window, factory) {
if (typeof exports === 'object') { if (typeof exports === 'object') {
// Node/CommonJS // Node/CommonJS
exports = module.exports = factory; exports = module.exports = factory();
} else if (typeof define === 'function' && define.amd) { } else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module. // AMD. Register as an anonymous module.
define(function() { define(factory);
return factory;
});
} else { } else {
// Browser globals // Browser globals
Handlebars.formHelpers = factory(Handlebars); window.HandlebarsFormHelpers = factory();
} }
}(function(Handlebars) { }(this, function factory() {


/* Common vars /* Global vars
*****************************************/ *****************************************/
var ns = '', form = 'form', input = 'input', label = 'label', var Handlebars, ns = '',

// Form strings
form = 'form', input = 'input', label = 'label',
button = 'button', submit = 'submit', select = 'select', option = 'option', button = 'button', submit = 'submit', select = 'select', option = 'option',
checkbox = 'checkbox', radio = 'radio', hidden = 'hidden', checkbox = 'checkbox', radio = 'radio', hidden = 'hidden',
textarea = 'textarea', password = 'password', file = 'file', textarea = 'textarea', password = 'password', file = 'file',

// Validation strings
validationErrorClass = 'validation-error', validationSufffix = '_validation', validationErrorClass = 'validation-error', validationSufffix = '_validation',
field_errors = 'field_errors'; field_errors = 'field_errors';


Expand Down Expand Up @@ -316,10 +319,14 @@
return ns; return ns;
} }
ns = setGetNs + (setGetNs ? '-' : ''); ns = setGetNs + (setGetNs ? '-' : '');
return this;
} }


// Register all helpers // Register all helpers
function register() { function register(HandlebarsSrc) {

Handlebars = HandlebarsSrc;

registerHelpers([ registerHelpers([
// Form helpers // Form helpers
[form, helperForm], [form, helperForm],
Expand Down

0 comments on commit eaa42be

Please sign in to comment.