Permalink
Browse files

Add 'attrs' and 'className' field schema attributes

  • Loading branch information...
1 parent 309c2c8 commit 354e6b2ebaf96d58d80c3ebceb98270e7cdc43ad @powmedia committed Mar 2, 2012
Showing with 44 additions and 0 deletions.
  1. +8 −0 README.md
  2. +6 −0 src/backbone-forms.js
  3. +30 −0 test/field.js
View
@@ -191,6 +191,14 @@ For each field definition in the schema you can use the following optional attri
- Help text to add next to the editor.
+**`className`**
+
+- String of CSS class name(s) to add to the field
+
+**`attrs`**
+
+- A map of attributes to add to the field, e.g. `{ style: 'background: red', title: 'Tooltip help' }`
+
<a name="editor-text"/>
View
@@ -640,6 +640,12 @@
this.$help.empty();
if (this.schema.help) this.$help.html(this.schema.help);
+ //Add custom CSS class names
+ if (this.schema.className) $field.addClass(this.schema.className);
+
+ //Add custom attributes
+ if (this.schema.attrs) $field.attr(this.schema.attrs);
+
this.setElement($field);
return this;
View
@@ -65,6 +65,36 @@ test("'schema.help' option - Specifies help text", function() {
equal($('.bbf-help', field.el).html(), 'Some new help text');
});
+test("'schema.className' option - Adds class names to field", function() {
+ var field = new Field({
+ key: 'title',
+ schema: { className: 'foo bar' }
+ }).render();
+
+ ok(field.$el.hasClass('bbf-field'), 'Doesnt overwrite default classes');
+ ok(field.$el.hasClass('foo'), 'Adds first defined class');
+ ok(field.$el.hasClass('bar'), 'Adds other defined class');
+})
+
+test("'schema.attrs' option - Adds custom attributes", function() {
+ var field = new Field({
+ key: 'title',
+ schema: {
+ attrs: {
+ maxlength: 30,
+ type: 'foo',
+ custom: 'hello'
+ }
+ }
+ }).render();
+
+ var $el = field.$el;
+
+ equal($el.attr('maxlength'), 30);
+ equal($el.attr('type'), 'foo');
+ equal($el.attr('custom'), 'hello');
+})
+
test("'model' option - Populates the field with the given 'key' option from the model", function() {
var field = new Field({
model: new Post,

0 comments on commit 354e6b2

Please sign in to comment.