Permalink
Browse files

Removed global Handlebars dependency, instead the Handlebars object i…

…s referenced in the register() method
  • Loading branch information...
1 parent 8d15b4f commit eaa42beb2ee2c3c30c64ac0976841202e6ce22e5 @badsyntax committed May 25, 2013
Showing with 39 additions and 36 deletions.
  1. +13 −13 README.md
  2. +1 −1 examples/form_validation.html
  3. +8 −12 spec/handlebars.form-helpers.spec.js
  4. +17 −10 src/handlebars.form-helpers.js
View
@@ -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)
[![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
### 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`
Load the scripts into your page, ensure you load the form helpers after handlebars:
@@ -20,7 +20,7 @@ Load the scripts into your page, ensure you load the form helpers after handleba
Then register the helpers:
```javascript
-Handlebars.formHelpers.register();
+HandlebarsFormHelpers.register(Handlebars);
```
### Node/CommonJS
@@ -32,29 +32,29 @@ dependency), as well as register the helpers:
```javascript
var hbs = require('hbs');
-require('handlebars-form-helpers')(hbs.handlebars).register();
+require('handlebars-form-helpers').register(hbs.handlebars);
```
### AMD
As with the CommonJS module, you need to initiate the AMD module manually, as well as register the helpers:
```javascript
-define(['handlebars', 'handlebars-form-helpers'], function(handlebars, handlebarsHelpersInit) {
- handlebarsHelpersInit(handlebars).register()''
+define(['handlebars', 'handlebars-form-helpers'], function(handlebars, handlebarsHelpers) {
+ handlebarsHelpers.register(handlebars);
// ..etc
});
```
## 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"}}
```
-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}}
@@ -63,16 +63,16 @@ The only block helpers are the form and field_errors helpers:
{{/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
the `namespace()` public API method to set a custom namespace, before registering the helpers, for example:
```javascript
-Handlebars.formHelpers.namespace('myform'); // set the namespace before registering
-Handlebars.formHelpers.register();
+HandlebarsFormHelpers.namespace('myform'); // set the namespace before registering
+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"}}
@@ -185,7 +185,7 @@
compile: function() {
// Register the form helpers
- Handlebars.formHelpers.register();
+ HandlebarsFormHelpers.register(Handlebars);
// Pre-compile the template
this.source = $('#form-template').html();
@@ -1,31 +1,27 @@
describe('Handlebars form helpers', function() {
- it('Depends on Handlebars', function() {
- expect(typeof Handlebars).not.toBe('undefined');
- });
-
describe('Public API', function() {
it('Has \'register\' and \'namespace\' methods', function() {
- expect(typeof Handlebars.formHelpers).toBe('object');
- expect(typeof Handlebars.formHelpers.register).toBe('function');
- expect(typeof Handlebars.formHelpers.namespace).toBe('function');
+ expect(typeof HandlebarsFormHelpers).toBe('object');
+ expect(typeof HandlebarsFormHelpers.register).toBe('function');
+ expect(typeof HandlebarsFormHelpers.namespace).toBe('function');
});
it('Sets or gets the namespace', function() {
- Handlebars.formHelpers.namespace('test');
- expect(Handlebars.formHelpers.namespace()).toBe('test-');
+ HandlebarsFormHelpers.namespace('test');
+ expect(HandlebarsFormHelpers.namespace()).toBe('test-');
});
it('Registers the form helpers with the namespace', function() {
// Reset the namespace after running this test
this.after(function() {
- Handlebars.formHelpers.namespace('');
- Handlebars.formHelpers.register();
+ HandlebarsFormHelpers.namespace('');
+ HandlebarsFormHelpers.register(Handlebars);
});
- Handlebars.formHelpers.register();
+ HandlebarsFormHelpers.register(Handlebars);
expect(typeof Handlebars.helpers['test-form']).not.toBe('undefined');
});
});
@@ -4,27 +4,30 @@
* Copyright (c) 2013 Richard Willis; Licensed MIT
*/
-(function (factory) {
+(function (window, factory) {
if (typeof exports === 'object') {
// Node/CommonJS
- exports = module.exports = factory;
+ exports = module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
- define(function() {
- return factory;
- });
+ define(factory);
} else {
// 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',
checkbox = 'checkbox', radio = 'radio', hidden = 'hidden',
textarea = 'textarea', password = 'password', file = 'file',
+
+ // Validation strings
validationErrorClass = 'validation-error', validationSufffix = '_validation',
field_errors = 'field_errors';
@@ -316,10 +319,14 @@
return ns;
}
ns = setGetNs + (setGetNs ? '-' : '');
+ return this;
}
// Register all helpers
- function register() {
+ function register(HandlebarsSrc) {
+
+ Handlebars = HandlebarsSrc;
+
registerHelpers([
// Form helpers
[form, helperForm],

0 comments on commit eaa42be

Please sign in to comment.