Permalink
Browse files

Add ability to specify form template. Closes #34.

  • Loading branch information...
1 parent 5f1ee18 commit 2bd6821085a749d7f764c3e96a0e24fdb0dcc983 @powmedia powmedia committed Mar 17, 2012
Showing with 30 additions and 1 deletion.
  1. +13 −0 README.md
  2. +5 −1 src/backbone-forms.js
  3. +12 −0 test/form.js
View
@@ -429,6 +429,19 @@ A string that will be prefixed to the form DOM element IDs. Useful if you will h
If not defined, the model's CID will be used as a prefix to avoid conflicts when there are multiple instances of the form on the page. To override this behaviour, pass a null value to `idPrefix`.
+**`template`**
+
+The template name to use for generating the form. E.g.:
+
+ Backbone.Form.setTemplates({
+ customForm: '<form class="custom-form">{{fieldsets}}</form>'
+ });
+
+ var form = new Backbone.Form({
+ model: user,
+ template: 'customForm'
+ });
+
[Back to top](#top)
View
@@ -330,6 +330,9 @@
* model {Backbone.Model} : Use instead of data, and use commit().
* data {Array} : Pass this when not using a model. Use getValue() to get out value
* fields {Array} : Keys of fields to include in the form, in display order (default: all fields)
+ * fieldsets {Array} : Allows choosing and ordering fields within fieldsets.
+ * idPrefix {String} : Prefix for editor IDs. If undefined, the model's CID is used.
+ * template {String} : Template to use. Default to 'form'.
*/
initialize: function(options) {
//Get the schema
@@ -349,6 +352,7 @@
this.data = options.data;
this.fieldsToRender = options.fields || _.keys(this.schema);
this.fieldsets = options.fieldsets;
+ this.templateName = options.template || 'form';
//Stores all Field views
this.fields = {};
@@ -364,7 +368,7 @@
templates = Form.templates;
//Create el from template
- var $form = $(templates.form({
+ var $form = $(templates[this.templateName]({
fieldsets: '<div class="bbf-placeholder"></div>'
}));
View
@@ -116,6 +116,18 @@ test("'idPrefix' option - Adds prefix to all DOM element IDs", function() {
equal($('#form_title', form.el).length, 1);
});
+test("'template' option - Specifies template", function() {
+ Form.templates.customForm = Form.helpers.createTemplate('<div class="custom-form"><form>{{fieldsets}}</form></div>');
+
+ var form = new Form({
+ model: new Post,
+ template: 'customForm'
+ }).render();
+
+ ok(form.$el.hasClass('custom-form'));
+})
+
+
test("validate() - validates the form and returns an errors object", function () {
var form = new Form({
schema: {

0 comments on commit 2bd6821

Please sign in to comment.