apostrophe-schemas
Advanced tools
Comparing version 0.5.5 to 0.5.6
28
index.js
@@ -118,2 +118,14 @@ var async = require('async'); | ||
_.each(schema, function(field) { | ||
if (field.template) { | ||
if (typeof(field.template) === 'string') { | ||
field.render = self.renderer(field.template); | ||
delete field.template; | ||
} else { | ||
field.render = field.template; | ||
delete field.template; | ||
} | ||
} | ||
}); | ||
return schema; | ||
@@ -132,3 +144,3 @@ }; | ||
// For custom types. For the builtin types we use macros. | ||
self.templates = {}; | ||
self.renders = {}; | ||
@@ -479,3 +491,5 @@ // BEGIN CONVERTERS | ||
self.addFieldType = function(type) { | ||
self.templates[type.name] = type.template; | ||
// template is accepted for bc but it was always a function, so | ||
// render is a much better name | ||
self.renders[type.name] = type.render || type.template; | ||
self.converters.csv[type.name] = type.converters.csv; | ||
@@ -488,6 +502,10 @@ self.converters.form[type.name] = type.converters.form; | ||
self._apos.addLocal('aposSchemaField', function(field) { | ||
if (!self.templates[field.type]) { | ||
throw "No such field type, or you forgot to set its template property when calling schemas.addFieldType, or it is a built-in type that has its own macro and you are calling aposSchemaField on it anyway: " + field.type; | ||
// Alow custom renderers for types and for individual fields | ||
var render = field.render || self.renders[field.type]; | ||
if (!render) { | ||
// Look for a standard render template in the views folder | ||
// of this module | ||
return self.renderer(field.type)(field); | ||
} | ||
return self.templates[field.type](field); | ||
return render(field); | ||
}); | ||
@@ -494,0 +512,0 @@ |
{ | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"name": "apostrophe-schemas", | ||
@@ -4,0 +4,0 @@ "description": "Schemas for easy editing of properties in Apostrophe objects", |
@@ -24,2 +24,3 @@ # apostrophe-schemas | ||
* [Accessing Relationship Properties in a Reverse Join](#accessing-relationship-properties-in-a-reverse-join) | ||
* [Overriding Templates For Individual Fields](#overriding-templates-for-individual-fields) | ||
* [Adding New Field Types](#adding-new-field-types) | ||
@@ -523,2 +524,32 @@ * Support for Subclassing and Mixins | ||
### Overriding Templates For Individual Fields | ||
You can override templates for individual fields without resorting to writing your own `new.html` and `edit.html` templates from scratch. | ||
Here's the `string.html` template that renders all fields with the `string` type by default: | ||
```html | ||
{% include "schemaMacros.html" %} | ||
{% if textarea %} | ||
{{ formTextarea(name, label) }} | ||
{% else %} | ||
{{ formText(name, label) }} | ||
{% endif %} | ||
``` | ||
You can override these for your project by creating new templates with the same names in the `lib/modules/apostrophe-schemas/views` folder. This lets you change the appearance for every field of a particular type. You should only override what you really wish to change. | ||
In addition, you can specify an alternate template name for an individual field in your schema: | ||
{ | ||
type: 'integer', | ||
name: 'shoeSize', | ||
label: 'Shoe Size', | ||
template: 'shoeSize' | ||
} | ||
This will cause the `shoeSize.html` template to be rendered instead of the `integer.html` template. | ||
You can also pass a `render` function, which receives the field object as its only parameter. Usually you'll find it much more convenient to just use a string and put your templates in `lib/modules/apostrophe-schemas/views`. | ||
### Adding New Field Types | ||
@@ -530,3 +561,3 @@ | ||
* A "template" method that just renders a suitable Nunjucks template to insert this type of field in a form. Browser-side JavaScript will populate it with content later. Use the assets mixin in your module to make this code easy to write. | ||
* A "render" method that just renders a suitable Nunjucks template to insert this type of field in a form. Browser-side JavaScript will populate it with content later. Use the assets mixin in your module to make this code easy to write. | ||
* A converter for use when a form submission arrives. | ||
@@ -547,3 +578,3 @@ * A converter for use during CSV import of an object. | ||
name: 'list', | ||
template: self.renderer('schemaList'), | ||
render: self.renderer('schemaList'), | ||
converters: { | ||
@@ -608,3 +639,3 @@ form: function(data, name, object, field) { | ||
Next, on the browser side, we need to supply three methods: a displayer and a converter. | ||
Next, on the browser side, we need to supply two methods: a displayer and a converter. | ||
@@ -611,0 +642,0 @@ "displayer" is a method that populates the form field. `aposSchemas.populateFields` will invoke it. |
Sorry, the diff of this file is not supported yet
71695
25
886
814